|
| 1 | +// ---------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// *** AUTO GENERATED CODE *** Type: MMv1 *** |
| 4 | +// |
| 5 | +// ---------------------------------------------------------------------------- |
| 6 | +// |
| 7 | +// This file is automatically generated by Magic Modules and manual |
| 8 | +// changes will be clobbered when the file is regenerated. |
| 9 | +// |
| 10 | +// Please read more about how to change this file in |
| 11 | +// .github/CONTRIBUTING.md. |
| 12 | +// |
| 13 | +// ---------------------------------------------------------------------------- |
| 14 | + |
| 15 | +package google |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + |
| 20 | + "github.com/hashicorp/errwrap" |
| 21 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 22 | + "google.golang.org/api/cloudresourcemanager/v1" |
| 23 | +) |
| 24 | + |
| 25 | +var SecurityCenterSourceIamSchema = map[string]*schema.Schema{ |
| 26 | + "organization": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Required: true, |
| 29 | + ForceNew: true, |
| 30 | + }, |
| 31 | + "source": { |
| 32 | + Type: schema.TypeString, |
| 33 | + Required: true, |
| 34 | + ForceNew: true, |
| 35 | + DiffSuppressFunc: compareSelfLinkOrResourceName, |
| 36 | + }, |
| 37 | +} |
| 38 | + |
| 39 | +type SecurityCenterSourceIamUpdater struct { |
| 40 | + organization string |
| 41 | + source string |
| 42 | + d TerraformResourceData |
| 43 | + Config *Config |
| 44 | +} |
| 45 | + |
| 46 | +func SecurityCenterSourceIamUpdaterProducer(d TerraformResourceData, config *Config) (ResourceIamUpdater, error) { |
| 47 | + values := make(map[string]string) |
| 48 | + |
| 49 | + if v, ok := d.GetOk("organization"); ok { |
| 50 | + values["organization"] = v.(string) |
| 51 | + } |
| 52 | + |
| 53 | + if v, ok := d.GetOk("source"); ok { |
| 54 | + values["source"] = v.(string) |
| 55 | + } |
| 56 | + |
| 57 | + // We may have gotten either a long or short name, so attempt to parse long name if possible |
| 58 | + m, err := getImportIdQualifiers([]string{"organizations/(?P<organization>[^/]+)/sources/(?P<source>[^/]+)", "(?P<organization>[^/]+)/(?P<source>[^/]+)", "(?P<source>[^/]+)"}, d, config, d.Get("source").(string)) |
| 59 | + if err != nil { |
| 60 | + return nil, err |
| 61 | + } |
| 62 | + |
| 63 | + for k, v := range m { |
| 64 | + values[k] = v |
| 65 | + } |
| 66 | + |
| 67 | + u := &SecurityCenterSourceIamUpdater{ |
| 68 | + organization: values["organization"], |
| 69 | + source: values["source"], |
| 70 | + d: d, |
| 71 | + Config: config, |
| 72 | + } |
| 73 | + |
| 74 | + if err := d.Set("organization", u.organization); err != nil { |
| 75 | + return nil, fmt.Errorf("Error setting organization: %s", err) |
| 76 | + } |
| 77 | + if err := d.Set("source", u.GetResourceId()); err != nil { |
| 78 | + return nil, fmt.Errorf("Error setting source: %s", err) |
| 79 | + } |
| 80 | + |
| 81 | + return u, nil |
| 82 | +} |
| 83 | + |
| 84 | +func SecurityCenterSourceIdParseFunc(d *schema.ResourceData, config *Config) error { |
| 85 | + values := make(map[string]string) |
| 86 | + |
| 87 | + m, err := getImportIdQualifiers([]string{"organizations/(?P<organization>[^/]+)/sources/(?P<source>[^/]+)", "(?P<organization>[^/]+)/(?P<source>[^/]+)", "(?P<source>[^/]+)"}, d, config, d.Id()) |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + |
| 92 | + for k, v := range m { |
| 93 | + values[k] = v |
| 94 | + } |
| 95 | + |
| 96 | + u := &SecurityCenterSourceIamUpdater{ |
| 97 | + organization: values["organization"], |
| 98 | + source: values["source"], |
| 99 | + d: d, |
| 100 | + Config: config, |
| 101 | + } |
| 102 | + if err := d.Set("source", u.GetResourceId()); err != nil { |
| 103 | + return fmt.Errorf("Error setting source: %s", err) |
| 104 | + } |
| 105 | + d.SetId(u.GetResourceId()) |
| 106 | + return nil |
| 107 | +} |
| 108 | + |
| 109 | +func (u *SecurityCenterSourceIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager.Policy, error) { |
| 110 | + url, err := u.qualifySourceUrl("getIamPolicy") |
| 111 | + if err != nil { |
| 112 | + return nil, err |
| 113 | + } |
| 114 | + |
| 115 | + var obj map[string]interface{} |
| 116 | + |
| 117 | + userAgent, err := generateUserAgentString(u.d, u.Config.userAgent) |
| 118 | + if err != nil { |
| 119 | + return nil, err |
| 120 | + } |
| 121 | + |
| 122 | + policy, err := sendRequest(u.Config, "POST", "", url, userAgent, obj) |
| 123 | + if err != nil { |
| 124 | + return nil, errwrap.Wrapf(fmt.Sprintf("Error retrieving IAM policy for %s: {{err}}", u.DescribeResource()), err) |
| 125 | + } |
| 126 | + |
| 127 | + out := &cloudresourcemanager.Policy{} |
| 128 | + err = Convert(policy, out) |
| 129 | + if err != nil { |
| 130 | + return nil, errwrap.Wrapf("Cannot convert a policy to a resource manager policy: {{err}}", err) |
| 131 | + } |
| 132 | + |
| 133 | + return out, nil |
| 134 | +} |
| 135 | + |
| 136 | +func (u *SecurityCenterSourceIamUpdater) SetResourceIamPolicy(policy *cloudresourcemanager.Policy) error { |
| 137 | + json, err := ConvertToMap(policy) |
| 138 | + if err != nil { |
| 139 | + return err |
| 140 | + } |
| 141 | + |
| 142 | + obj := make(map[string]interface{}) |
| 143 | + obj["policy"] = json |
| 144 | + |
| 145 | + url, err := u.qualifySourceUrl("setIamPolicy") |
| 146 | + if err != nil { |
| 147 | + return err |
| 148 | + } |
| 149 | + |
| 150 | + userAgent, err := generateUserAgentString(u.d, u.Config.userAgent) |
| 151 | + if err != nil { |
| 152 | + return err |
| 153 | + } |
| 154 | + |
| 155 | + _, err = sendRequestWithTimeout(u.Config, "POST", "", url, userAgent, obj, u.d.Timeout(schema.TimeoutCreate)) |
| 156 | + if err != nil { |
| 157 | + return errwrap.Wrapf(fmt.Sprintf("Error setting IAM policy for %s: {{err}}", u.DescribeResource()), err) |
| 158 | + } |
| 159 | + |
| 160 | + return nil |
| 161 | +} |
| 162 | + |
| 163 | +func (u *SecurityCenterSourceIamUpdater) qualifySourceUrl(methodIdentifier string) (string, error) { |
| 164 | + urlTemplate := fmt.Sprintf("{{SecurityCenterBasePath}}%s:%s", fmt.Sprintf("organizations/%s/sources/%s", u.organization, u.source), methodIdentifier) |
| 165 | + url, err := replaceVars(u.d, u.Config, urlTemplate) |
| 166 | + if err != nil { |
| 167 | + return "", err |
| 168 | + } |
| 169 | + return url, nil |
| 170 | +} |
| 171 | + |
| 172 | +func (u *SecurityCenterSourceIamUpdater) GetResourceId() string { |
| 173 | + return fmt.Sprintf("organizations/%s/sources/%s", u.organization, u.source) |
| 174 | +} |
| 175 | + |
| 176 | +func (u *SecurityCenterSourceIamUpdater) GetMutexKey() string { |
| 177 | + return fmt.Sprintf("iam-securitycenter-source-%s", u.GetResourceId()) |
| 178 | +} |
| 179 | + |
| 180 | +func (u *SecurityCenterSourceIamUpdater) DescribeResource() string { |
| 181 | + return fmt.Sprintf("securitycenter source %q", u.GetResourceId()) |
| 182 | +} |
0 commit comments