|
| 1 | +// ---------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** |
| 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 | +package google |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + |
| 19 | + "github.com/hashicorp/errwrap" |
| 20 | + "github.com/hashicorp/terraform/helper/schema" |
| 21 | + "google.golang.org/api/cloudresourcemanager/v1" |
| 22 | +) |
| 23 | + |
| 24 | +var RuntimeConfigConfigIamSchema = map[string]*schema.Schema{ |
| 25 | + "project": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Computed: true, |
| 28 | + Optional: true, |
| 29 | + ForceNew: true, |
| 30 | + }, |
| 31 | + "config": { |
| 32 | + Type: schema.TypeString, |
| 33 | + Required: true, |
| 34 | + ForceNew: true, |
| 35 | + DiffSuppressFunc: compareSelfLinkOrResourceName, |
| 36 | + }, |
| 37 | +} |
| 38 | + |
| 39 | +type RuntimeConfigConfigIamUpdater struct { |
| 40 | + project string |
| 41 | + config string |
| 42 | + d *schema.ResourceData |
| 43 | + Config *Config |
| 44 | +} |
| 45 | + |
| 46 | +func RuntimeConfigConfigIamUpdaterProducer(d *schema.ResourceData, config *Config) (ResourceIamUpdater, error) { |
| 47 | + values := make(map[string]string) |
| 48 | + |
| 49 | + project, err := getProject(d, config) |
| 50 | + if err != nil { |
| 51 | + return nil, err |
| 52 | + } |
| 53 | + values["project"] = project |
| 54 | + |
| 55 | + // We may have gotten either a long or short name, so attempt to parse long name if possible |
| 56 | + m, err := getImportIdQualifiers([]string{"projects/(?P<project>[^/]+)/configs/(?P<config>[^/]+)", "(?P<project>[^/]+)/(?P<config>[^/]+)", "(?P<config>[^/]+)"}, d, config, d.Get("config").(string)) |
| 57 | + if err != nil { |
| 58 | + return nil, err |
| 59 | + } |
| 60 | + |
| 61 | + for k, v := range m { |
| 62 | + values[k] = v |
| 63 | + } |
| 64 | + |
| 65 | + u := &RuntimeConfigConfigIamUpdater{ |
| 66 | + project: values["project"], |
| 67 | + config: values["config"], |
| 68 | + d: d, |
| 69 | + Config: config, |
| 70 | + } |
| 71 | + |
| 72 | + d.Set("project", u.project) |
| 73 | + d.Set("config", u.GetResourceId()) |
| 74 | + |
| 75 | + d.SetId(u.GetResourceId()) |
| 76 | + |
| 77 | + return u, nil |
| 78 | +} |
| 79 | + |
| 80 | +func RuntimeConfigConfigIdParseFunc(d *schema.ResourceData, config *Config) error { |
| 81 | + values := make(map[string]string) |
| 82 | + |
| 83 | + project, err := getProject(d, config) |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + values["project"] = project |
| 88 | + |
| 89 | + m, err := getImportIdQualifiers([]string{"projects/(?P<project>[^/]+)/configs/(?P<config>[^/]+)", "(?P<project>[^/]+)/(?P<config>[^/]+)", "(?P<config>[^/]+)"}, d, config, d.Id()) |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + |
| 94 | + for k, v := range m { |
| 95 | + values[k] = v |
| 96 | + } |
| 97 | + |
| 98 | + u := &RuntimeConfigConfigIamUpdater{ |
| 99 | + project: values["project"], |
| 100 | + config: values["config"], |
| 101 | + d: d, |
| 102 | + Config: config, |
| 103 | + } |
| 104 | + d.Set("config", u.GetResourceId()) |
| 105 | + d.SetId(u.GetResourceId()) |
| 106 | + return nil |
| 107 | +} |
| 108 | + |
| 109 | +func (u *RuntimeConfigConfigIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager.Policy, error) { |
| 110 | + url := u.qualifyConfigUrl("getIamPolicy") |
| 111 | + |
| 112 | + project, err := getProject(u.d, u.Config) |
| 113 | + if err != nil { |
| 114 | + return nil, err |
| 115 | + } |
| 116 | + |
| 117 | + policy, err := sendRequest(u.Config, "GET", project, url, nil) |
| 118 | + if err != nil { |
| 119 | + return nil, errwrap.Wrapf(fmt.Sprintf("Error retrieving IAM policy for %s: {{err}}", u.DescribeResource()), err) |
| 120 | + } |
| 121 | + |
| 122 | + out := &cloudresourcemanager.Policy{} |
| 123 | + err = Convert(policy, out) |
| 124 | + if err != nil { |
| 125 | + return nil, errwrap.Wrapf("Cannot convert a policy to a resource manager policy: {{err}}", err) |
| 126 | + } |
| 127 | + |
| 128 | + return out, nil |
| 129 | +} |
| 130 | + |
| 131 | +func (u *RuntimeConfigConfigIamUpdater) SetResourceIamPolicy(policy *cloudresourcemanager.Policy) error { |
| 132 | + json, err := ConvertToMap(policy) |
| 133 | + if err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + |
| 137 | + obj := make(map[string]interface{}) |
| 138 | + obj["policy"] = json |
| 139 | + |
| 140 | + url := u.qualifyConfigUrl("setIamPolicy") |
| 141 | + |
| 142 | + project, err := getProject(u.d, u.Config) |
| 143 | + if err != nil { |
| 144 | + return err |
| 145 | + } |
| 146 | + |
| 147 | + _, err = sendRequestWithTimeout(u.Config, "POST", project, url, obj, u.d.Timeout(schema.TimeoutCreate)) |
| 148 | + if err != nil { |
| 149 | + return errwrap.Wrapf(fmt.Sprintf("Error setting IAM policy for %s: {{err}}", u.DescribeResource()), err) |
| 150 | + } |
| 151 | + |
| 152 | + return nil |
| 153 | +} |
| 154 | + |
| 155 | +func (u *RuntimeConfigConfigIamUpdater) qualifyConfigUrl(methodIdentifier string) string { |
| 156 | + return fmt.Sprintf("https://runtimeconfig.googleapis.com/v1beta1/%s:%s", fmt.Sprintf("projects/%s/configs/%s", u.project, u.config), methodIdentifier) |
| 157 | +} |
| 158 | + |
| 159 | +func (u *RuntimeConfigConfigIamUpdater) GetResourceId() string { |
| 160 | + return fmt.Sprintf("projects/%s/configs/%s", u.project, u.config) |
| 161 | +} |
| 162 | + |
| 163 | +func (u *RuntimeConfigConfigIamUpdater) GetMutexKey() string { |
| 164 | + return fmt.Sprintf("iam-runtimeconfig-config-%s", u.GetResourceId()) |
| 165 | +} |
| 166 | + |
| 167 | +func (u *RuntimeConfigConfigIamUpdater) DescribeResource() string { |
| 168 | + return fmt.Sprintf("runtimeconfig config %q", u.GetResourceId()) |
| 169 | +} |
0 commit comments