diff --git a/converters/google/resources/gke_hub_membership_iam.go b/converters/google/resources/gke_hub_membership_iam.go new file mode 100644 index 000000000..190f3f7b1 --- /dev/null +++ b/converters/google/resources/gke_hub_membership_iam.go @@ -0,0 +1,120 @@ +// ---------------------------------------------------------------------------- +// +// *** 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 google + +import "fmt" + +// Provide a separate asset type constant so we don't have to worry about name conflicts between IAM and non-IAM converter files +const GKEHubMembershipIAMAssetType string = "gkehub.googleapis.com/Membership" + +func resourceConverterGKEHubMembershipIamPolicy() ResourceConverter { + return ResourceConverter{ + AssetType: GKEHubMembershipIAMAssetType, + Convert: GetGKEHubMembershipIamPolicyCaiObject, + MergeCreateUpdate: MergeGKEHubMembershipIamPolicy, + } +} + +func resourceConverterGKEHubMembershipIamBinding() ResourceConverter { + return ResourceConverter{ + AssetType: GKEHubMembershipIAMAssetType, + Convert: GetGKEHubMembershipIamBindingCaiObject, + FetchFullResource: FetchGKEHubMembershipIamPolicy, + MergeCreateUpdate: MergeGKEHubMembershipIamBinding, + MergeDelete: MergeGKEHubMembershipIamBindingDelete, + } +} + +func resourceConverterGKEHubMembershipIamMember() ResourceConverter { + return ResourceConverter{ + AssetType: GKEHubMembershipIAMAssetType, + Convert: GetGKEHubMembershipIamMemberCaiObject, + FetchFullResource: FetchGKEHubMembershipIamPolicy, + MergeCreateUpdate: MergeGKEHubMembershipIamMember, + MergeDelete: MergeGKEHubMembershipIamMemberDelete, + } +} + +func GetGKEHubMembershipIamPolicyCaiObject(d TerraformResourceData, config *Config) ([]Asset, error) { + return newGKEHubMembershipIamAsset(d, config, expandIamPolicyBindings) +} + +func GetGKEHubMembershipIamBindingCaiObject(d TerraformResourceData, config *Config) ([]Asset, error) { + return newGKEHubMembershipIamAsset(d, config, expandIamRoleBindings) +} + +func GetGKEHubMembershipIamMemberCaiObject(d TerraformResourceData, config *Config) ([]Asset, error) { + return newGKEHubMembershipIamAsset(d, config, expandIamMemberBindings) +} + +func MergeGKEHubMembershipIamPolicy(existing, incoming Asset) Asset { + existing.IAMPolicy = incoming.IAMPolicy + return existing +} + +func MergeGKEHubMembershipIamBinding(existing, incoming Asset) Asset { + return mergeIamAssets(existing, incoming, mergeAuthoritativeBindings) +} + +func MergeGKEHubMembershipIamBindingDelete(existing, incoming Asset) Asset { + return mergeDeleteIamAssets(existing, incoming, mergeDeleteAuthoritativeBindings) +} + +func MergeGKEHubMembershipIamMember(existing, incoming Asset) Asset { + return mergeIamAssets(existing, incoming, mergeAdditiveBindings) +} + +func MergeGKEHubMembershipIamMemberDelete(existing, incoming Asset) Asset { + return mergeDeleteIamAssets(existing, incoming, mergeDeleteAdditiveBindings) +} + +func newGKEHubMembershipIamAsset( + d TerraformResourceData, + config *Config, + expandBindings func(d TerraformResourceData) ([]IAMBinding, error), +) ([]Asset, error) { + bindings, err := expandBindings(d) + if err != nil { + return []Asset{}, fmt.Errorf("expanding bindings: %v", err) + } + + name, err := assetName(d, config, "//gkehub.googleapis.com/projects/{{project}}/locations/{{location}}/memberships/{{membership_id}}") + if err != nil { + return []Asset{}, err + } + + return []Asset{{ + Name: name, + Type: GKEHubMembershipIAMAssetType, + IAMPolicy: &IAMPolicy{ + Bindings: bindings, + }, + }}, nil +} + +func FetchGKEHubMembershipIamPolicy(d TerraformResourceData, config *Config) (Asset, error) { + // Check if the identity field returns a value + if _, ok := d.GetOk("membership_id"); !ok { + return Asset{}, ErrEmptyIdentityField + } + + return fetchIamPolicy( + GKEHubMembershipIamUpdaterProducer, + d, + config, + "//gkehub.googleapis.com/projects/{{project}}/locations/{{location}}/memberships/{{membership_id}}", + GKEHubMembershipIAMAssetType, + ) +} diff --git a/converters/google/resources/iam_gke_hub_membership.go b/converters/google/resources/iam_gke_hub_membership.go new file mode 100644 index 000000000..25ed167af --- /dev/null +++ b/converters/google/resources/iam_gke_hub_membership.go @@ -0,0 +1,198 @@ +// ---------------------------------------------------------------------------- +// +// *** 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 google + +import ( + "fmt" + + "github.com/hashicorp/errwrap" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "google.golang.org/api/cloudresourcemanager/v1" +) + +var GKEHubMembershipIamSchema = map[string]*schema.Schema{ + "project": { + Type: schema.TypeString, + Computed: true, + Optional: true, + ForceNew: true, + }, + "membership_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + DiffSuppressFunc: compareSelfLinkOrResourceName, + }, +} + +type GKEHubMembershipIamUpdater struct { + project string + membershipId string + d TerraformResourceData + Config *Config +} + +func GKEHubMembershipIamUpdaterProducer(d TerraformResourceData, config *Config) (ResourceIamUpdater, error) { + values := make(map[string]string) + + project, _ := 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 + if v, ok := d.GetOk("membership_id"); ok { + values["membership_id"] = v.(string) + } + + // We may have gotten either a long or short name, so attempt to parse long name if possible + m, err := getImportIdQualifiers([]string{"projects/(?P[^/]+)/locations/(?P[^/]+)/memberships/(?P[^/]+)", "(?P[^/]+)/(?P[^/]+)/(?P[^/]+)", "(?P[^/]+)/(?P[^/]+)", "(?P[^/]+)"}, d, config, d.Get("membership_id").(string)) + if err != nil { + return nil, err + } + + for k, v := range m { + values[k] = v + } + + u := &GKEHubMembershipIamUpdater{ + project: values["project"], + membershipId: values["membership_id"], + 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("membership_id", u.GetResourceId()); err != nil { + return nil, fmt.Errorf("Error setting membership_id: %s", err) + } + + return u, nil +} + +func GKEHubMembershipIdParseFunc(d *schema.ResourceData, config *Config) error { + values := make(map[string]string) + + project, _ := getProject(d, config) + if project != "" { + values["project"] = project + } + + m, err := getImportIdQualifiers([]string{"projects/(?P[^/]+)/locations/(?P[^/]+)/memberships/(?P[^/]+)", "(?P[^/]+)/(?P[^/]+)/(?P[^/]+)", "(?P[^/]+)/(?P[^/]+)", "(?P[^/]+)"}, d, config, d.Id()) + if err != nil { + return err + } + + for k, v := range m { + values[k] = v + } + + u := &GKEHubMembershipIamUpdater{ + project: values["project"], + membershipId: values["membership_id"], + d: d, + Config: config, + } + if err := d.Set("membership_id", u.GetResourceId()); err != nil { + return fmt.Errorf("Error setting membership_id: %s", err) + } + d.SetId(u.GetResourceId()) + return nil +} + +func (u *GKEHubMembershipIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager.Policy, error) { + url, err := u.qualifyMembershipUrl("getIamPolicy") + if err != nil { + return nil, err + } + + project, err := getProject(u.d, u.Config) + if err != nil { + return nil, err + } + var obj map[string]interface{} + + userAgent, err := generateUserAgentString(u.d, u.Config.userAgent) + if err != nil { + return nil, err + } + + policy, err := sendRequest(u.Config, "GET", project, url, userAgent, obj) + if err != nil { + return nil, errwrap.Wrapf(fmt.Sprintf("Error retrieving IAM policy for %s: {{err}}", u.DescribeResource()), err) + } + + out := &cloudresourcemanager.Policy{} + err = 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 *GKEHubMembershipIamUpdater) SetResourceIamPolicy(policy *cloudresourcemanager.Policy) error { + json, err := ConvertToMap(policy) + if err != nil { + return err + } + + obj := make(map[string]interface{}) + obj["policy"] = json + + url, err := u.qualifyMembershipUrl("setIamPolicy") + if err != nil { + return err + } + project, err := getProject(u.d, u.Config) + if err != nil { + return err + } + + userAgent, err := generateUserAgentString(u.d, u.Config.userAgent) + if err != nil { + return err + } + + _, err = sendRequestWithTimeout(u.Config, "POST", project, url, userAgent, obj, 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 *GKEHubMembershipIamUpdater) qualifyMembershipUrl(methodIdentifier string) (string, error) { + urlTemplate := fmt.Sprintf("{{GKEHubBasePath}}%s:%s", fmt.Sprintf("projects/%s/locations/global/memberships/%s", u.project, u.membershipId), methodIdentifier) + url, err := replaceVars(u.d, u.Config, urlTemplate) + if err != nil { + return "", err + } + return url, nil +} + +func (u *GKEHubMembershipIamUpdater) GetResourceId() string { + return fmt.Sprintf("projects/%s/locations/global/memberships/%s", u.project, u.membershipId) +} + +func (u *GKEHubMembershipIamUpdater) GetMutexKey() string { + return fmt.Sprintf("iam-gkehub-membership-%s", u.GetResourceId()) +} + +func (u *GKEHubMembershipIamUpdater) DescribeResource() string { + return fmt.Sprintf("gkehub membership %q", u.GetResourceId()) +} diff --git a/converters/google/resources/resource_converters.go b/converters/google/resources/resource_converters.go index 869dc0e05..d4ecf6be5 100644 --- a/converters/google/resources/resource_converters.go +++ b/converters/google/resources/resource_converters.go @@ -148,6 +148,9 @@ func ResourceConverters() map[string][]ResourceConverter { "google_dataproc_metastore_service_iam_policy": {resourceConverterDataprocMetastoreServiceIamPolicy()}, "google_dataproc_metastore_service_iam_binding": {resourceConverterDataprocMetastoreServiceIamBinding()}, "google_dataproc_metastore_service_iam_member": {resourceConverterDataprocMetastoreServiceIamMember()}, + "google_gke_hub_membership_iam_policy": {resourceConverterGKEHubMembershipIamPolicy()}, + "google_gke_hub_membership_iam_binding": {resourceConverterGKEHubMembershipIamBinding()}, + "google_gke_hub_membership_iam_member": {resourceConverterGKEHubMembershipIamMember()}, "google_healthcare_consent_store_iam_policy": {resourceConverterHealthcareConsentStoreIamPolicy()}, "google_healthcare_consent_store_iam_binding": {resourceConverterHealthcareConsentStoreIamBinding()}, "google_healthcare_consent_store_iam_member": {resourceConverterHealthcareConsentStoreIamMember()}, diff --git a/go.mod b/go.mod index 642bda566..6baa09db8 100644 --- a/go.mod +++ b/go.mod @@ -26,7 +26,7 @@ require ( github.com/hashicorp/go-cleanhttp v0.5.2 github.com/hashicorp/terraform-json v0.14.0 github.com/hashicorp/terraform-plugin-sdk/v2 v2.18.0 - github.com/hashicorp/terraform-provider-google v1.20.1-0.20220805233107-e92447f36743 + github.com/hashicorp/terraform-provider-google v1.20.1-0.20220808210210-69e2aa222636 github.com/mitchellh/go-homedir v1.1.0 github.com/pkg/errors v0.9.1 github.com/sirupsen/logrus v1.8.1 diff --git a/go.sum b/go.sum index 8acaf527d..204217124 100644 --- a/go.sum +++ b/go.sum @@ -642,8 +642,8 @@ github.com/hashicorp/terraform-plugin-log v0.4.1 h1:xpbmVhvuU3mgHzLetOmx9pkOL2rm github.com/hashicorp/terraform-plugin-log v0.4.1/go.mod h1:p4R1jWBXRTvL4odmEkFfDdhUjHf9zcs/BCoNHAc7IK4= github.com/hashicorp/terraform-plugin-sdk/v2 v2.18.0 h1:/cdI5di5XA+N80gXzXF4YcHq36DprBskubk6Z8i26ZQ= github.com/hashicorp/terraform-plugin-sdk/v2 v2.18.0/go.mod h1:L3SHkD/Q8zPVgXviQmpVwy9nKwpXXZscVIpVEnQ/T50= -github.com/hashicorp/terraform-provider-google v1.20.1-0.20220805233107-e92447f36743 h1:BxdIBTcHgXVweXFWbFvRNI0MnsqjCMn3sdQjXL3Brco= -github.com/hashicorp/terraform-provider-google v1.20.1-0.20220805233107-e92447f36743/go.mod h1:HJ4eeT50GEXVlW89O0pxY505sHy2X6JoqnPu2A8n/lY= +github.com/hashicorp/terraform-provider-google v1.20.1-0.20220808210210-69e2aa222636 h1:KH7RL0RMY3pHLA7aJResXGY6PAk6EPBmV3Xe6UawrYQ= +github.com/hashicorp/terraform-provider-google v1.20.1-0.20220808210210-69e2aa222636/go.mod h1:HJ4eeT50GEXVlW89O0pxY505sHy2X6JoqnPu2A8n/lY= github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c h1:D8aRO6+mTqHfLsK/BC3j5OAoogv1WLRWzY1AaTo3rBg= github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c/go.mod h1:Wn3Na71knbXc1G8Lh+yu/dQWWJeFQEpDeJMtWMtlmNI= github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 h1:HKLsbzeOsfXmKNpr3GiT18XAblV0BjCbzL8KQAMZGa0=