Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding RestorePlan to Backup for GKE #6278

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/8803.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
`google_gke_backup_restore_plan`
```
11 changes: 8 additions & 3 deletions google-beta/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,7 @@ func DatasourceMapWithErrors() (map[string]*schema.Resource, error) {
"google_dataproc_metastore_service_iam_policy": tpgiamresource.DataSourceIamPolicy(dataprocmetastore.DataprocMetastoreServiceIamSchema, dataprocmetastore.DataprocMetastoreServiceIamUpdaterProducer),
"google_dns_managed_zone_iam_policy": tpgiamresource.DataSourceIamPolicy(dns.DNSManagedZoneIamSchema, dns.DNSManagedZoneIamUpdaterProducer),
"google_gke_backup_backup_plan_iam_policy": tpgiamresource.DataSourceIamPolicy(gkebackup.GKEBackupBackupPlanIamSchema, gkebackup.GKEBackupBackupPlanIamUpdaterProducer),
"google_gke_backup_restore_plan_iam_policy": tpgiamresource.DataSourceIamPolicy(gkebackup.GKEBackupRestorePlanIamSchema, gkebackup.GKEBackupRestorePlanIamUpdaterProducer),
"google_gke_hub_membership_iam_policy": tpgiamresource.DataSourceIamPolicy(gkehub.GKEHubMembershipIamSchema, gkehub.GKEHubMembershipIamUpdaterProducer),
"google_gke_hub_feature_iam_policy": tpgiamresource.DataSourceIamPolicy(gkehub2.GKEHub2FeatureIamSchema, gkehub2.GKEHub2FeatureIamUpdaterProducer),
"google_gke_hub_scope_iam_policy": tpgiamresource.DataSourceIamPolicy(gkehub2.GKEHub2ScopeIamSchema, gkehub2.GKEHub2ScopeIamUpdaterProducer),
Expand Down Expand Up @@ -1114,9 +1115,9 @@ func DatasourceMapWithErrors() (map[string]*schema.Resource, error) {
})
}

// Generated resources: 384
// Generated IAM resources: 237
// Total generated resources: 621
// Generated resources: 385
// Generated IAM resources: 240
// Total generated resources: 625
func ResourceMap() map[string]*schema.Resource {
resourceMap, _ := ResourceMapWithErrors()
return resourceMap
Expand Down Expand Up @@ -1508,6 +1509,10 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) {
"google_gke_backup_backup_plan_iam_binding": tpgiamresource.ResourceIamBinding(gkebackup.GKEBackupBackupPlanIamSchema, gkebackup.GKEBackupBackupPlanIamUpdaterProducer, gkebackup.GKEBackupBackupPlanIdParseFunc),
"google_gke_backup_backup_plan_iam_member": tpgiamresource.ResourceIamMember(gkebackup.GKEBackupBackupPlanIamSchema, gkebackup.GKEBackupBackupPlanIamUpdaterProducer, gkebackup.GKEBackupBackupPlanIdParseFunc),
"google_gke_backup_backup_plan_iam_policy": tpgiamresource.ResourceIamPolicy(gkebackup.GKEBackupBackupPlanIamSchema, gkebackup.GKEBackupBackupPlanIamUpdaterProducer, gkebackup.GKEBackupBackupPlanIdParseFunc),
"google_gke_backup_restore_plan": gkebackup.ResourceGKEBackupRestorePlan(),
"google_gke_backup_restore_plan_iam_binding": tpgiamresource.ResourceIamBinding(gkebackup.GKEBackupRestorePlanIamSchema, gkebackup.GKEBackupRestorePlanIamUpdaterProducer, gkebackup.GKEBackupRestorePlanIdParseFunc),
"google_gke_backup_restore_plan_iam_member": tpgiamresource.ResourceIamMember(gkebackup.GKEBackupRestorePlanIamSchema, gkebackup.GKEBackupRestorePlanIamUpdaterProducer, gkebackup.GKEBackupRestorePlanIdParseFunc),
"google_gke_backup_restore_plan_iam_policy": tpgiamresource.ResourceIamPolicy(gkebackup.GKEBackupRestorePlanIamSchema, gkebackup.GKEBackupRestorePlanIamUpdaterProducer, gkebackup.GKEBackupRestorePlanIdParseFunc),
"google_gke_hub_membership": gkehub.ResourceGKEHubMembership(),
"google_gke_hub_membership_iam_binding": tpgiamresource.ResourceIamBinding(gkehub.GKEHubMembershipIamSchema, gkehub.GKEHubMembershipIamUpdaterProducer, gkehub.GKEHubMembershipIdParseFunc),
"google_gke_hub_membership_iam_member": tpgiamresource.ResourceIamMember(gkehub.GKEHubMembershipIamSchema, gkehub.GKEHubMembershipIamUpdaterProducer, gkehub.GKEHubMembershipIdParseFunc),
Expand Down
245 changes: 245 additions & 0 deletions google-beta/services/gkebackup/iam_gke_backup_restore_plan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** Type: MMv1 ***
//
// ----------------------------------------------------------------------------
//
// This file is automatically generated by Magic Modules and manual
// changes will be clobbered when the file is regenerated.
//
// Please read more about how to change this file in
// .github/CONTRIBUTING.md.
//
// ----------------------------------------------------------------------------

package gkebackup

import (
"fmt"

"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"google.golang.org/api/cloudresourcemanager/v1"

"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgiamresource"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
)

var GKEBackupRestorePlanIamSchema = map[string]*schema.Schema{
"project": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
},
"location": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: tpgresource.CompareSelfLinkOrResourceName,
},
}

type GKEBackupRestorePlanIamUpdater struct {
project string
location string
name string
d tpgresource.TerraformResourceData
Config *transport_tpg.Config
}

func GKEBackupRestorePlanIamUpdaterProducer(d tpgresource.TerraformResourceData, config *transport_tpg.Config) (tpgiamresource.ResourceIamUpdater, error) {
values := make(map[string]string)

project, _ := tpgresource.GetProject(d, config)
if project != "" {
if err := d.Set("project", project); err != nil {
return nil, fmt.Errorf("Error setting project: %s", err)
}
}
values["project"] = project
location, _ := tpgresource.GetLocation(d, config)
if location != "" {
if err := d.Set("location", location); err != nil {
return nil, fmt.Errorf("Error setting location: %s", err)
}
}
values["location"] = location
if v, ok := d.GetOk("name"); ok {
values["name"] = v.(string)
}

// We may have gotten either a long or short name, so attempt to parse long name if possible
m, err := tpgresource.GetImportIdQualifiers([]string{"projects/(?P<project>[^/]+)/locations/(?P<location>[^/]+)/restorePlans/(?P<name>[^/]+)", "(?P<project>[^/]+)/(?P<location>[^/]+)/(?P<name>[^/]+)", "(?P<location>[^/]+)/(?P<name>[^/]+)", "(?P<name>[^/]+)"}, d, config, d.Get("name").(string))
if err != nil {
return nil, err
}

for k, v := range m {
values[k] = v
}

u := &GKEBackupRestorePlanIamUpdater{
project: values["project"],
location: values["location"],
name: values["name"],
d: d,
Config: config,
}

if err := d.Set("project", u.project); err != nil {
return nil, fmt.Errorf("Error setting project: %s", err)
}
if err := d.Set("location", u.location); err != nil {
return nil, fmt.Errorf("Error setting location: %s", err)
}
if err := d.Set("name", u.GetResourceId()); err != nil {
return nil, fmt.Errorf("Error setting name: %s", err)
}

return u, nil
}

func GKEBackupRestorePlanIdParseFunc(d *schema.ResourceData, config *transport_tpg.Config) error {
values := make(map[string]string)

project, _ := tpgresource.GetProject(d, config)
if project != "" {
values["project"] = project
}

location, _ := tpgresource.GetLocation(d, config)
if location != "" {
values["location"] = location
}

m, err := tpgresource.GetImportIdQualifiers([]string{"projects/(?P<project>[^/]+)/locations/(?P<location>[^/]+)/restorePlans/(?P<name>[^/]+)", "(?P<project>[^/]+)/(?P<location>[^/]+)/(?P<name>[^/]+)", "(?P<location>[^/]+)/(?P<name>[^/]+)", "(?P<name>[^/]+)"}, d, config, d.Id())
if err != nil {
return err
}

for k, v := range m {
values[k] = v
}

u := &GKEBackupRestorePlanIamUpdater{
project: values["project"],
location: values["location"],
name: values["name"],
d: d,
Config: config,
}
if err := d.Set("name", u.GetResourceId()); err != nil {
return fmt.Errorf("Error setting name: %s", err)
}
d.SetId(u.GetResourceId())
return nil
}

func (u *GKEBackupRestorePlanIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager.Policy, error) {
url, err := u.qualifyRestorePlanUrl("getIamPolicy")
if err != nil {
return nil, err
}

project, err := tpgresource.GetProject(u.d, u.Config)
if err != nil {
return nil, err
}
var obj map[string]interface{}

userAgent, err := tpgresource.GenerateUserAgentString(u.d, u.Config.UserAgent)
if err != nil {
return nil, err
}

policy, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: u.Config,
Method: "GET",
Project: project,
RawURL: url,
UserAgent: userAgent,
Body: obj,
})
if err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("Error retrieving IAM policy for %s: {{err}}", u.DescribeResource()), err)
}

out := &cloudresourcemanager.Policy{}
err = tpgresource.Convert(policy, out)
if err != nil {
return nil, errwrap.Wrapf("Cannot convert a policy to a resource manager policy: {{err}}", err)
}

return out, nil
}

func (u *GKEBackupRestorePlanIamUpdater) SetResourceIamPolicy(policy *cloudresourcemanager.Policy) error {
json, err := tpgresource.ConvertToMap(policy)
if err != nil {
return err
}

obj := make(map[string]interface{})
obj["policy"] = json

url, err := u.qualifyRestorePlanUrl("setIamPolicy")
if err != nil {
return err
}
project, err := tpgresource.GetProject(u.d, u.Config)
if err != nil {
return err
}

userAgent, err := tpgresource.GenerateUserAgentString(u.d, u.Config.UserAgent)
if err != nil {
return err
}

_, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: u.Config,
Method: "POST",
Project: project,
RawURL: url,
UserAgent: userAgent,
Body: obj,
Timeout: u.d.Timeout(schema.TimeoutCreate),
})
if err != nil {
return errwrap.Wrapf(fmt.Sprintf("Error setting IAM policy for %s: {{err}}", u.DescribeResource()), err)
}

return nil
}

func (u *GKEBackupRestorePlanIamUpdater) qualifyRestorePlanUrl(methodIdentifier string) (string, error) {
urlTemplate := fmt.Sprintf("{{GKEBackupBasePath}}%s:%s", fmt.Sprintf("projects/%s/locations/%s/restorePlans/%s", u.project, u.location, u.name), methodIdentifier)
url, err := tpgresource.ReplaceVars(u.d, u.Config, urlTemplate)
if err != nil {
return "", err
}
return url, nil
}

func (u *GKEBackupRestorePlanIamUpdater) GetResourceId() string {
return fmt.Sprintf("projects/%s/locations/%s/restorePlans/%s", u.project, u.location, u.name)
}

func (u *GKEBackupRestorePlanIamUpdater) GetMutexKey() string {
return fmt.Sprintf("iam-gkebackup-restoreplan-%s", u.GetResourceId())
}

func (u *GKEBackupRestorePlanIamUpdater) DescribeResource() string {
return fmt.Sprintf("gkebackup restoreplan %q", u.GetResourceId())
}
Loading