Skip to content

Commit

Permalink
Organization Source iam (#10881) (#2612)
Browse files Browse the repository at this point in the history
[upstream:8299248f6ef98ecddc4d6678c70d4584b6cf0eb7]

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Aug 6, 2024
1 parent 233ddfc commit 42e9fcb
Show file tree
Hide file tree
Showing 6 changed files with 414 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/hashicorp/hcl/v2 v2.20.1
github.com/hashicorp/terraform-json v0.22.1
github.com/hashicorp/terraform-plugin-sdk/v2 v2.33.0
github.com/hashicorp/terraform-provider-google-beta v1.20.1-0.20240806160509-7ec00f79745f
github.com/hashicorp/terraform-provider-google-beta v1.20.1-0.20240806174006-7bdebcc397c0
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.9.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ github.com/hashicorp/terraform-plugin-sdk/v2 v2.33.0 h1:qHprzXy/As0rxedphECBEQAh
github.com/hashicorp/terraform-plugin-sdk/v2 v2.33.0/go.mod h1:H+8tjs9TjV2w57QFVSMBQacf8k/E1XwLXGCARgViC6A=
github.com/hashicorp/terraform-plugin-testing v1.5.1 h1:T4aQh9JAhmWo4+t1A7x+rnxAJHCDIYW9kXyo4sVO92c=
github.com/hashicorp/terraform-plugin-testing v1.5.1/go.mod h1:dg8clO6K59rZ8w9EshBmDp1CxTIPu3yA4iaDpX1h5u0=
github.com/hashicorp/terraform-provider-google-beta v1.20.1-0.20240806160509-7ec00f79745f h1:FpDucLi3DIfJJIPYHLdE47gW8VLePH5oD9qa/AuiFpE=
github.com/hashicorp/terraform-provider-google-beta v1.20.1-0.20240806160509-7ec00f79745f/go.mod h1:xXX9Llw2TT08vR/UZK9p2GZjCm0vsvos2Vhi3ikZIi0=
github.com/hashicorp/terraform-provider-google-beta v1.20.1-0.20240806174006-7bdebcc397c0 h1:EJ89H+0KksETIMi+UNDvPORrDm+X30oeBqXOUI3xr44=
github.com/hashicorp/terraform-provider-google-beta v1.20.1-0.20240806174006-7bdebcc397c0/go.mod h1:xXX9Llw2TT08vR/UZK9p2GZjCm0vsvos2Vhi3ikZIi0=
github.com/hashicorp/terraform-registry-address v0.2.3 h1:2TAiKJ1A3MAkZlH1YI/aTVcLZRu7JseiXNRHbOAyoTI=
github.com/hashicorp/terraform-registry-address v0.2.3/go.mod h1:lFHA76T8jfQteVfT7caREqguFrW3c4MFSPhZB7HHgUM=
github.com/hashicorp/terraform-svchost v0.1.1 h1:EZZimZ1GxdqFRinZ1tpJwVxxt49xc/S52uzrw4x0jKQ=
Expand Down
4 changes: 4 additions & 0 deletions tfplan2cai/converters/google/resources/resource_converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import (
"github.com/GoogleCloudPlatform/terraform-google-conversion/v5/tfplan2cai/converters/google/resources/services/secretmanager"
"github.com/GoogleCloudPlatform/terraform-google-conversion/v5/tfplan2cai/converters/google/resources/services/securesourcemanager"
"github.com/GoogleCloudPlatform/terraform-google-conversion/v5/tfplan2cai/converters/google/resources/services/securitycenter"
"github.com/GoogleCloudPlatform/terraform-google-conversion/v5/tfplan2cai/converters/google/resources/services/securitycenterv2"
"github.com/GoogleCloudPlatform/terraform-google-conversion/v5/tfplan2cai/converters/google/resources/services/servicemanagement"
"github.com/GoogleCloudPlatform/terraform-google-conversion/v5/tfplan2cai/converters/google/resources/services/spanner"
"github.com/GoogleCloudPlatform/terraform-google-conversion/v5/tfplan2cai/converters/google/resources/services/sql"
Expand Down Expand Up @@ -371,6 +372,9 @@ func ResourceConverters() map[string][]cai.ResourceConverter {
"google_scc_source_iam_policy": {securitycenter.ResourceConverterSecurityCenterSourceIamPolicy()},
"google_scc_source_iam_binding": {securitycenter.ResourceConverterSecurityCenterSourceIamBinding()},
"google_scc_source_iam_member": {securitycenter.ResourceConverterSecurityCenterSourceIamMember()},
"google_scc_v2_organization_source_iam_policy": {securitycenterv2.ResourceConverterSecurityCenterV2OrganizationSourceIamPolicy()},
"google_scc_v2_organization_source_iam_binding": {securitycenterv2.ResourceConverterSecurityCenterV2OrganizationSourceIamBinding()},
"google_scc_v2_organization_source_iam_member": {securitycenterv2.ResourceConverterSecurityCenterV2OrganizationSourceIamMember()},
"google_endpoints_service_iam_policy": {servicemanagement.ResourceConverterServiceManagementServiceIamPolicy()},
"google_endpoints_service_iam_binding": {servicemanagement.ResourceConverterServiceManagementServiceIamBinding()},
"google_endpoints_service_iam_member": {servicemanagement.ResourceConverterServiceManagementServiceIamMember()},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
// ----------------------------------------------------------------------------
//
// *** 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 securitycenterv2

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 SecurityCenterV2OrganizationSourceIamSchema = map[string]*schema.Schema{
"organization": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"source": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: tpgresource.CompareSelfLinkOrResourceName,
},
}

type SecurityCenterV2OrganizationSourceIamUpdater struct {
organization string
source string
d tpgresource.TerraformResourceData
Config *transport_tpg.Config
}

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

if v, ok := d.GetOk("organization"); ok {
values["organization"] = v.(string)
}

if v, ok := d.GetOk("source"); ok {
values["source"] = 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{"organizations/(?P<organization>[^/]+)/sources/(?P<source>[^/]+)", "(?P<organization>[^/]+)/(?P<source>[^/]+)", "(?P<source>[^/]+)"}, d, config, d.Get("source").(string))
if err != nil {
return nil, err
}

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

u := &SecurityCenterV2OrganizationSourceIamUpdater{
organization: values["organization"],
source: values["source"],
d: d,
Config: config,
}

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

return u, nil
}

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

m, err := tpgresource.GetImportIdQualifiers([]string{"organizations/(?P<organization>[^/]+)/sources/(?P<source>[^/]+)", "(?P<organization>[^/]+)/(?P<source>[^/]+)", "(?P<source>[^/]+)"}, d, config, d.Id())
if err != nil {
return err
}

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

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

func (u *SecurityCenterV2OrganizationSourceIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager.Policy, error) {
url, err := u.qualifyOrganizationSourceUrl("getIamPolicy")
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: "POST",
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 *SecurityCenterV2OrganizationSourceIamUpdater) 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.qualifyOrganizationSourceUrl("setIamPolicy")
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",
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 *SecurityCenterV2OrganizationSourceIamUpdater) qualifyOrganizationSourceUrl(methodIdentifier string) (string, error) {
urlTemplate := fmt.Sprintf("{{SecurityCenterV2BasePath}}%s:%s", fmt.Sprintf("organizations/%s/sources/%s", u.organization, u.source), methodIdentifier)
url, err := tpgresource.ReplaceVars(u.d, u.Config, urlTemplate)
if err != nil {
return "", err
}
return url, nil
}

func (u *SecurityCenterV2OrganizationSourceIamUpdater) GetResourceId() string {
return fmt.Sprintf("organizations/%s/sources/%s", u.organization, u.source)
}

func (u *SecurityCenterV2OrganizationSourceIamUpdater) GetMutexKey() string {
return fmt.Sprintf("iam-securitycenterv2-organizationsource-%s", u.GetResourceId())
}

func (u *SecurityCenterV2OrganizationSourceIamUpdater) DescribeResource() string {
return fmt.Sprintf("securitycenterv2 organizationsource %q", u.GetResourceId())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// ----------------------------------------------------------------------------
//
// *** 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 securitycenterv2

import (
"reflect"

"github.com/GoogleCloudPlatform/terraform-google-conversion/v5/tfplan2cai/converters/google/resources/cai"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
)

const SecurityCenterV2OrganizationSourceAssetType string = "securitycenter.googleapis.com/OrganizationSource"

func ResourceConverterSecurityCenterV2OrganizationSource() cai.ResourceConverter {
return cai.ResourceConverter{
AssetType: SecurityCenterV2OrganizationSourceAssetType,
Convert: GetSecurityCenterV2OrganizationSourceCaiObject,
}
}

func GetSecurityCenterV2OrganizationSourceCaiObject(d tpgresource.TerraformResourceData, config *transport_tpg.Config) ([]cai.Asset, error) {
name, err := cai.AssetName(d, config, "//securitycenter.googleapis.com/{{name}}")
if err != nil {
return []cai.Asset{}, err
}
if obj, err := GetSecurityCenterV2OrganizationSourceApiObject(d, config); err == nil {
return []cai.Asset{{
Name: name,
Type: SecurityCenterV2OrganizationSourceAssetType,
Resource: &cai.AssetResource{
Version: "v2",
DiscoveryDocumentURI: "https://www.googleapis.com/discovery/v1/apis/securitycenter/v2/rest",
DiscoveryName: "OrganizationSource",
Data: obj,
},
}}, nil
} else {
return []cai.Asset{}, err
}
}

func GetSecurityCenterV2OrganizationSourceApiObject(d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]interface{}, error) {
obj := make(map[string]interface{})
descriptionProp, err := expandSecurityCenterV2OrganizationSourceDescription(d.Get("description"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("description"); !tpgresource.IsEmptyValue(reflect.ValueOf(descriptionProp)) && (ok || !reflect.DeepEqual(v, descriptionProp)) {
obj["description"] = descriptionProp
}
displayNameProp, err := expandSecurityCenterV2OrganizationSourceDisplayName(d.Get("display_name"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("display_name"); !tpgresource.IsEmptyValue(reflect.ValueOf(displayNameProp)) && (ok || !reflect.DeepEqual(v, displayNameProp)) {
obj["displayName"] = displayNameProp
}

return obj, nil
}

func expandSecurityCenterV2OrganizationSourceDescription(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandSecurityCenterV2OrganizationSourceDisplayName(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}
Loading

0 comments on commit 42e9fcb

Please sign in to comment.