|
| 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 | + "log" |
| 20 | + "reflect" |
| 21 | + "strings" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 25 | +) |
| 26 | + |
| 27 | +func resourceIdentityPlatformConfig() *schema.Resource { |
| 28 | + return &schema.Resource{ |
| 29 | + Create: resourceIdentityPlatformConfigCreate, |
| 30 | + Read: resourceIdentityPlatformConfigRead, |
| 31 | + Update: resourceIdentityPlatformConfigUpdate, |
| 32 | + Delete: resourceIdentityPlatformConfigDelete, |
| 33 | + |
| 34 | + Importer: &schema.ResourceImporter{ |
| 35 | + State: resourceIdentityPlatformConfigImport, |
| 36 | + }, |
| 37 | + |
| 38 | + Timeouts: &schema.ResourceTimeout{ |
| 39 | + Create: schema.DefaultTimeout(20 * time.Minute), |
| 40 | + Update: schema.DefaultTimeout(20 * time.Minute), |
| 41 | + Delete: schema.DefaultTimeout(20 * time.Minute), |
| 42 | + }, |
| 43 | + |
| 44 | + Schema: map[string]*schema.Schema{ |
| 45 | + "autodelete_anonymous_users": { |
| 46 | + Type: schema.TypeBool, |
| 47 | + Optional: true, |
| 48 | + Description: `Whether anonymous users will be auto-deleted after a period of 30 days`, |
| 49 | + }, |
| 50 | + "name": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Computed: true, |
| 53 | + Description: `The name of the Config resource`, |
| 54 | + }, |
| 55 | + "project": { |
| 56 | + Type: schema.TypeString, |
| 57 | + Optional: true, |
| 58 | + Computed: true, |
| 59 | + ForceNew: true, |
| 60 | + }, |
| 61 | + }, |
| 62 | + UseJSONNumber: true, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func resourceIdentityPlatformConfigCreate(d *schema.ResourceData, meta interface{}) error { |
| 67 | + config := meta.(*Config) |
| 68 | + userAgent, err := generateUserAgentString(d, config.userAgent) |
| 69 | + if err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + |
| 73 | + obj := make(map[string]interface{}) |
| 74 | + autodeleteAnonymousUsersProp, err := expandIdentityPlatformConfigAutodeleteAnonymousUsers(d.Get("autodelete_anonymous_users"), d, config) |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } else if v, ok := d.GetOkExists("autodelete_anonymous_users"); !isEmptyValue(reflect.ValueOf(autodeleteAnonymousUsersProp)) && (ok || !reflect.DeepEqual(v, autodeleteAnonymousUsersProp)) { |
| 78 | + obj["autodeleteAnonymousUsers"] = autodeleteAnonymousUsersProp |
| 79 | + } |
| 80 | + |
| 81 | + url, err := replaceVars(d, config, "{{IdentityPlatformBasePath}}projects/{{project}}/identityPlatform:initializeAuth") |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + |
| 86 | + log.Printf("[DEBUG] Creating new Config: %#v", obj) |
| 87 | + billingProject := "" |
| 88 | + |
| 89 | + project, err := getProject(d, config) |
| 90 | + if err != nil { |
| 91 | + return fmt.Errorf("Error fetching project for Config: %s", err) |
| 92 | + } |
| 93 | + billingProject = project |
| 94 | + |
| 95 | + // err == nil indicates that the billing_project value was found |
| 96 | + if bp, err := getBillingProject(d, config); err == nil { |
| 97 | + billingProject = bp |
| 98 | + } |
| 99 | + |
| 100 | + res, err := sendRequestWithTimeout(config, "POST", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutCreate)) |
| 101 | + if err != nil { |
| 102 | + return fmt.Errorf("Error creating Config: %s", err) |
| 103 | + } |
| 104 | + if err := d.Set("name", flattenIdentityPlatformConfigName(res["name"], d, config)); err != nil { |
| 105 | + return fmt.Errorf(`Error setting computed identity field "name": %s`, err) |
| 106 | + } |
| 107 | + |
| 108 | + // Store the ID now |
| 109 | + id, err := replaceVars(d, config, "projects/{{project}}/config") |
| 110 | + if err != nil { |
| 111 | + return fmt.Errorf("Error constructing id: %s", err) |
| 112 | + } |
| 113 | + d.SetId(id) |
| 114 | + |
| 115 | + log.Printf("[DEBUG] Finished creating Config %q: %#v", d.Id(), res) |
| 116 | + |
| 117 | + return resourceIdentityPlatformConfigRead(d, meta) |
| 118 | +} |
| 119 | + |
| 120 | +func resourceIdentityPlatformConfigRead(d *schema.ResourceData, meta interface{}) error { |
| 121 | + config := meta.(*Config) |
| 122 | + userAgent, err := generateUserAgentString(d, config.userAgent) |
| 123 | + if err != nil { |
| 124 | + return err |
| 125 | + } |
| 126 | + |
| 127 | + url, err := replaceVars(d, config, "{{IdentityPlatformBasePath}}projects/{{project}}/config") |
| 128 | + if err != nil { |
| 129 | + return err |
| 130 | + } |
| 131 | + |
| 132 | + billingProject := "" |
| 133 | + |
| 134 | + project, err := getProject(d, config) |
| 135 | + if err != nil { |
| 136 | + return fmt.Errorf("Error fetching project for Config: %s", err) |
| 137 | + } |
| 138 | + billingProject = project |
| 139 | + |
| 140 | + // err == nil indicates that the billing_project value was found |
| 141 | + if bp, err := getBillingProject(d, config); err == nil { |
| 142 | + billingProject = bp |
| 143 | + } |
| 144 | + |
| 145 | + res, err := sendRequest(config, "GET", billingProject, url, userAgent, nil) |
| 146 | + if err != nil { |
| 147 | + return handleNotFoundError(err, d, fmt.Sprintf("IdentityPlatformConfig %q", d.Id())) |
| 148 | + } |
| 149 | + |
| 150 | + if err := d.Set("project", project); err != nil { |
| 151 | + return fmt.Errorf("Error reading Config: %s", err) |
| 152 | + } |
| 153 | + |
| 154 | + if err := d.Set("name", flattenIdentityPlatformConfigName(res["name"], d, config)); err != nil { |
| 155 | + return fmt.Errorf("Error reading Config: %s", err) |
| 156 | + } |
| 157 | + if err := d.Set("autodelete_anonymous_users", flattenIdentityPlatformConfigAutodeleteAnonymousUsers(res["autodeleteAnonymousUsers"], d, config)); err != nil { |
| 158 | + return fmt.Errorf("Error reading Config: %s", err) |
| 159 | + } |
| 160 | + |
| 161 | + return nil |
| 162 | +} |
| 163 | + |
| 164 | +func resourceIdentityPlatformConfigUpdate(d *schema.ResourceData, meta interface{}) error { |
| 165 | + config := meta.(*Config) |
| 166 | + userAgent, err := generateUserAgentString(d, config.userAgent) |
| 167 | + if err != nil { |
| 168 | + return err |
| 169 | + } |
| 170 | + |
| 171 | + billingProject := "" |
| 172 | + |
| 173 | + project, err := getProject(d, config) |
| 174 | + if err != nil { |
| 175 | + return fmt.Errorf("Error fetching project for Config: %s", err) |
| 176 | + } |
| 177 | + billingProject = project |
| 178 | + |
| 179 | + obj := make(map[string]interface{}) |
| 180 | + autodeleteAnonymousUsersProp, err := expandIdentityPlatformConfigAutodeleteAnonymousUsers(d.Get("autodelete_anonymous_users"), d, config) |
| 181 | + if err != nil { |
| 182 | + return err |
| 183 | + } else if v, ok := d.GetOkExists("autodelete_anonymous_users"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, autodeleteAnonymousUsersProp)) { |
| 184 | + obj["autodeleteAnonymousUsers"] = autodeleteAnonymousUsersProp |
| 185 | + } |
| 186 | + |
| 187 | + url, err := replaceVars(d, config, "{{IdentityPlatformBasePath}}projects/{{project}}/config") |
| 188 | + if err != nil { |
| 189 | + return err |
| 190 | + } |
| 191 | + |
| 192 | + log.Printf("[DEBUG] Updating Config %q: %#v", d.Id(), obj) |
| 193 | + updateMask := []string{} |
| 194 | + |
| 195 | + if d.HasChange("autodelete_anonymous_users") { |
| 196 | + updateMask = append(updateMask, "autodeleteAnonymousUsers") |
| 197 | + } |
| 198 | + // updateMask is a URL parameter but not present in the schema, so replaceVars |
| 199 | + // won't set it |
| 200 | + url, err = addQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")}) |
| 201 | + if err != nil { |
| 202 | + return err |
| 203 | + } |
| 204 | + |
| 205 | + // err == nil indicates that the billing_project value was found |
| 206 | + if bp, err := getBillingProject(d, config); err == nil { |
| 207 | + billingProject = bp |
| 208 | + } |
| 209 | + |
| 210 | + res, err := sendRequestWithTimeout(config, "PATCH", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutUpdate)) |
| 211 | + |
| 212 | + if err != nil { |
| 213 | + return fmt.Errorf("Error updating Config %q: %s", d.Id(), err) |
| 214 | + } else { |
| 215 | + log.Printf("[DEBUG] Finished updating Config %q: %#v", d.Id(), res) |
| 216 | + } |
| 217 | + |
| 218 | + return resourceIdentityPlatformConfigRead(d, meta) |
| 219 | +} |
| 220 | + |
| 221 | +func resourceIdentityPlatformConfigDelete(d *schema.ResourceData, meta interface{}) error { |
| 222 | + log.Printf("[WARNING] IdentityPlatform Config resources"+ |
| 223 | + " cannot be deleted from Google Cloud. The resource %s will be removed from Terraform"+ |
| 224 | + " state, but will still be present on Google Cloud.", d.Id()) |
| 225 | + d.SetId("") |
| 226 | + |
| 227 | + return nil |
| 228 | +} |
| 229 | + |
| 230 | +func resourceIdentityPlatformConfigImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { |
| 231 | + config := meta.(*Config) |
| 232 | + if err := parseImportId([]string{ |
| 233 | + "projects/(?P<project>[^/]+)/config", |
| 234 | + "projects/(?P<project>[^/]+)", |
| 235 | + "(?P<project>[^/]+)", |
| 236 | + }, d, config); err != nil { |
| 237 | + return nil, err |
| 238 | + } |
| 239 | + |
| 240 | + // Replace import id for the resource id |
| 241 | + id, err := replaceVars(d, config, "projects/{{project}}/config") |
| 242 | + if err != nil { |
| 243 | + return nil, fmt.Errorf("Error constructing id: %s", err) |
| 244 | + } |
| 245 | + d.SetId(id) |
| 246 | + |
| 247 | + return []*schema.ResourceData{d}, nil |
| 248 | +} |
| 249 | + |
| 250 | +func flattenIdentityPlatformConfigName(v interface{}, d *schema.ResourceData, config *Config) interface{} { |
| 251 | + return v |
| 252 | +} |
| 253 | + |
| 254 | +func flattenIdentityPlatformConfigAutodeleteAnonymousUsers(v interface{}, d *schema.ResourceData, config *Config) interface{} { |
| 255 | + return v |
| 256 | +} |
| 257 | + |
| 258 | +func expandIdentityPlatformConfigAutodeleteAnonymousUsers(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { |
| 259 | + return v, nil |
| 260 | +} |
0 commit comments