|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +// ---------------------------------------------------------------------------- |
| 5 | +// |
| 6 | +// *** AUTO GENERATED CODE *** Type: MMv1 *** |
| 7 | +// |
| 8 | +// ---------------------------------------------------------------------------- |
| 9 | +// |
| 10 | +// This file is automatically generated by Magic Modules and manual |
| 11 | +// changes will be clobbered when the file is regenerated. |
| 12 | +// |
| 13 | +// Please read more about how to change this file in |
| 14 | +// .github/CONTRIBUTING.md. |
| 15 | +// |
| 16 | +// ---------------------------------------------------------------------------- |
| 17 | + |
| 18 | +package discoveryengine |
| 19 | + |
| 20 | +import ( |
| 21 | + "fmt" |
| 22 | + "log" |
| 23 | + "net/http" |
| 24 | + "reflect" |
| 25 | + "time" |
| 26 | + |
| 27 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff" |
| 28 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 29 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure" |
| 30 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 31 | + |
| 32 | + "github.com/hashicorp/terraform-provider-google/google/tpgresource" |
| 33 | + transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" |
| 34 | +) |
| 35 | + |
| 36 | +func ResourceDiscoveryEngineSchema() *schema.Resource { |
| 37 | + return &schema.Resource{ |
| 38 | + Create: resourceDiscoveryEngineSchemaCreate, |
| 39 | + Read: resourceDiscoveryEngineSchemaRead, |
| 40 | + Delete: resourceDiscoveryEngineSchemaDelete, |
| 41 | + |
| 42 | + Importer: &schema.ResourceImporter{ |
| 43 | + State: resourceDiscoveryEngineSchemaImport, |
| 44 | + }, |
| 45 | + |
| 46 | + Timeouts: &schema.ResourceTimeout{ |
| 47 | + Create: schema.DefaultTimeout(60 * time.Minute), |
| 48 | + Delete: schema.DefaultTimeout(60 * time.Minute), |
| 49 | + }, |
| 50 | + |
| 51 | + CustomizeDiff: customdiff.All( |
| 52 | + tpgresource.DefaultProviderProject, |
| 53 | + ), |
| 54 | + |
| 55 | + Schema: map[string]*schema.Schema{ |
| 56 | + "data_store_id": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Required: true, |
| 59 | + ForceNew: true, |
| 60 | + Description: `The unique id of the data store.`, |
| 61 | + }, |
| 62 | + "location": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Required: true, |
| 65 | + ForceNew: true, |
| 66 | + Description: `The geographic location where the data store should reside. The value can |
| 67 | +only be one of "global", "us" and "eu".`, |
| 68 | + }, |
| 69 | + "schema_id": { |
| 70 | + Type: schema.TypeString, |
| 71 | + Required: true, |
| 72 | + ForceNew: true, |
| 73 | + Description: `The unique id of the schema.`, |
| 74 | + }, |
| 75 | + "json_schema": { |
| 76 | + Type: schema.TypeString, |
| 77 | + Optional: true, |
| 78 | + ForceNew: true, |
| 79 | + ValidateFunc: validation.StringIsJSON, |
| 80 | + StateFunc: func(v interface{}) string { s, _ := structure.NormalizeJsonString(v); return s }, |
| 81 | + Description: `The JSON representation of the schema.`, |
| 82 | + ExactlyOneOf: []string{"json_schema"}, |
| 83 | + }, |
| 84 | + "name": { |
| 85 | + Type: schema.TypeString, |
| 86 | + Computed: true, |
| 87 | + Description: `The unique full resource name of the schema. Values are of the format |
| 88 | +'projects/{project}/locations/{location}/collections/{collection_id}/dataStores/{data_store_id}/schemas/{schema_id}'. |
| 89 | +This field must be a UTF-8 encoded string with a length limit of 1024 |
| 90 | +characters.`, |
| 91 | + }, |
| 92 | + "project": { |
| 93 | + Type: schema.TypeString, |
| 94 | + Optional: true, |
| 95 | + Computed: true, |
| 96 | + ForceNew: true, |
| 97 | + }, |
| 98 | + }, |
| 99 | + UseJSONNumber: true, |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func resourceDiscoveryEngineSchemaCreate(d *schema.ResourceData, meta interface{}) error { |
| 104 | + config := meta.(*transport_tpg.Config) |
| 105 | + userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + |
| 110 | + obj := make(map[string]interface{}) |
| 111 | + jsonSchemaProp, err := expandDiscoveryEngineSchemaJsonSchema(d.Get("json_schema"), d, config) |
| 112 | + if err != nil { |
| 113 | + return err |
| 114 | + } else if v, ok := d.GetOkExists("json_schema"); !tpgresource.IsEmptyValue(reflect.ValueOf(jsonSchemaProp)) && (ok || !reflect.DeepEqual(v, jsonSchemaProp)) { |
| 115 | + obj["jsonSchema"] = jsonSchemaProp |
| 116 | + } |
| 117 | + |
| 118 | + url, err := tpgresource.ReplaceVars(d, config, "{{DiscoveryEngineBasePath}}projects/{{project}}/locations/{{location}}/collections/default_collection/dataStores/{{data_store_id}}/schemas?schemaId={{schema_id}}") |
| 119 | + if err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + |
| 123 | + log.Printf("[DEBUG] Creating new Schema: %#v", obj) |
| 124 | + billingProject := "" |
| 125 | + |
| 126 | + project, err := tpgresource.GetProject(d, config) |
| 127 | + if err != nil { |
| 128 | + return fmt.Errorf("Error fetching project for Schema: %s", err) |
| 129 | + } |
| 130 | + billingProject = project |
| 131 | + |
| 132 | + // err == nil indicates that the billing_project value was found |
| 133 | + if bp, err := tpgresource.GetBillingProject(d, config); err == nil { |
| 134 | + billingProject = bp |
| 135 | + } |
| 136 | + |
| 137 | + headers := make(http.Header) |
| 138 | + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| 139 | + Config: config, |
| 140 | + Method: "POST", |
| 141 | + Project: billingProject, |
| 142 | + RawURL: url, |
| 143 | + UserAgent: userAgent, |
| 144 | + Body: obj, |
| 145 | + Timeout: d.Timeout(schema.TimeoutCreate), |
| 146 | + Headers: headers, |
| 147 | + }) |
| 148 | + if err != nil { |
| 149 | + return fmt.Errorf("Error creating Schema: %s", err) |
| 150 | + } |
| 151 | + |
| 152 | + // Store the ID now |
| 153 | + id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/collections/default_collection/dataStores/{{data_store_id}}/schemas/{{schema_id}}") |
| 154 | + if err != nil { |
| 155 | + return fmt.Errorf("Error constructing id: %s", err) |
| 156 | + } |
| 157 | + d.SetId(id) |
| 158 | + |
| 159 | + err = DiscoveryEngineOperationWaitTime( |
| 160 | + config, res, project, "Creating Schema", userAgent, |
| 161 | + d.Timeout(schema.TimeoutCreate)) |
| 162 | + |
| 163 | + if err != nil { |
| 164 | + // The resource didn't actually create |
| 165 | + d.SetId("") |
| 166 | + return fmt.Errorf("Error waiting to create Schema: %s", err) |
| 167 | + } |
| 168 | + |
| 169 | + log.Printf("[DEBUG] Finished creating Schema %q: %#v", d.Id(), res) |
| 170 | + |
| 171 | + return resourceDiscoveryEngineSchemaRead(d, meta) |
| 172 | +} |
| 173 | + |
| 174 | +func resourceDiscoveryEngineSchemaRead(d *schema.ResourceData, meta interface{}) error { |
| 175 | + config := meta.(*transport_tpg.Config) |
| 176 | + userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| 177 | + if err != nil { |
| 178 | + return err |
| 179 | + } |
| 180 | + |
| 181 | + url, err := tpgresource.ReplaceVars(d, config, "{{DiscoveryEngineBasePath}}projects/{{project}}/locations/{{location}}/collections/default_collection/dataStores/{{data_store_id}}/schemas/{{schema_id}}") |
| 182 | + if err != nil { |
| 183 | + return err |
| 184 | + } |
| 185 | + |
| 186 | + billingProject := "" |
| 187 | + |
| 188 | + project, err := tpgresource.GetProject(d, config) |
| 189 | + if err != nil { |
| 190 | + return fmt.Errorf("Error fetching project for Schema: %s", err) |
| 191 | + } |
| 192 | + billingProject = project |
| 193 | + |
| 194 | + // err == nil indicates that the billing_project value was found |
| 195 | + if bp, err := tpgresource.GetBillingProject(d, config); err == nil { |
| 196 | + billingProject = bp |
| 197 | + } |
| 198 | + |
| 199 | + headers := make(http.Header) |
| 200 | + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| 201 | + Config: config, |
| 202 | + Method: "GET", |
| 203 | + Project: billingProject, |
| 204 | + RawURL: url, |
| 205 | + UserAgent: userAgent, |
| 206 | + Headers: headers, |
| 207 | + }) |
| 208 | + if err != nil { |
| 209 | + return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("DiscoveryEngineSchema %q", d.Id())) |
| 210 | + } |
| 211 | + |
| 212 | + if err := d.Set("project", project); err != nil { |
| 213 | + return fmt.Errorf("Error reading Schema: %s", err) |
| 214 | + } |
| 215 | + |
| 216 | + if err := d.Set("name", flattenDiscoveryEngineSchemaName(res["name"], d, config)); err != nil { |
| 217 | + return fmt.Errorf("Error reading Schema: %s", err) |
| 218 | + } |
| 219 | + if err := d.Set("json_schema", flattenDiscoveryEngineSchemaJsonSchema(res["jsonSchema"], d, config)); err != nil { |
| 220 | + return fmt.Errorf("Error reading Schema: %s", err) |
| 221 | + } |
| 222 | + |
| 223 | + return nil |
| 224 | +} |
| 225 | + |
| 226 | +func resourceDiscoveryEngineSchemaDelete(d *schema.ResourceData, meta interface{}) error { |
| 227 | + config := meta.(*transport_tpg.Config) |
| 228 | + userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| 229 | + if err != nil { |
| 230 | + return err |
| 231 | + } |
| 232 | + |
| 233 | + billingProject := "" |
| 234 | + |
| 235 | + project, err := tpgresource.GetProject(d, config) |
| 236 | + if err != nil { |
| 237 | + return fmt.Errorf("Error fetching project for Schema: %s", err) |
| 238 | + } |
| 239 | + billingProject = project |
| 240 | + |
| 241 | + url, err := tpgresource.ReplaceVars(d, config, "{{DiscoveryEngineBasePath}}projects/{{project}}/locations/{{location}}/collections/default_collection/dataStores/{{data_store_id}}/schemas/{{schema_id}}") |
| 242 | + if err != nil { |
| 243 | + return err |
| 244 | + } |
| 245 | + |
| 246 | + var obj map[string]interface{} |
| 247 | + |
| 248 | + // err == nil indicates that the billing_project value was found |
| 249 | + if bp, err := tpgresource.GetBillingProject(d, config); err == nil { |
| 250 | + billingProject = bp |
| 251 | + } |
| 252 | + |
| 253 | + headers := make(http.Header) |
| 254 | + |
| 255 | + log.Printf("[DEBUG] Deleting Schema %q", d.Id()) |
| 256 | + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| 257 | + Config: config, |
| 258 | + Method: "DELETE", |
| 259 | + Project: billingProject, |
| 260 | + RawURL: url, |
| 261 | + UserAgent: userAgent, |
| 262 | + Body: obj, |
| 263 | + Timeout: d.Timeout(schema.TimeoutDelete), |
| 264 | + Headers: headers, |
| 265 | + }) |
| 266 | + if err != nil { |
| 267 | + return transport_tpg.HandleNotFoundError(err, d, "Schema") |
| 268 | + } |
| 269 | + |
| 270 | + err = DiscoveryEngineOperationWaitTime( |
| 271 | + config, res, project, "Deleting Schema", userAgent, |
| 272 | + d.Timeout(schema.TimeoutDelete)) |
| 273 | + |
| 274 | + if err != nil { |
| 275 | + return err |
| 276 | + } |
| 277 | + |
| 278 | + log.Printf("[DEBUG] Finished deleting Schema %q: %#v", d.Id(), res) |
| 279 | + return nil |
| 280 | +} |
| 281 | + |
| 282 | +func resourceDiscoveryEngineSchemaImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { |
| 283 | + config := meta.(*transport_tpg.Config) |
| 284 | + if err := tpgresource.ParseImportId([]string{ |
| 285 | + "^projects/(?P<project>[^/]+)/locations/(?P<location>[^/]+)/collections/default_collection/dataStores/(?P<data_store_id>[^/]+)/schemas/(?P<schema_id>[^/]+)$", |
| 286 | + "^(?P<project>[^/]+)/(?P<location>[^/]+)/(?P<data_store_id>[^/]+)/(?P<schema_id>[^/]+)$", |
| 287 | + "^(?P<location>[^/]+)/(?P<data_store_id>[^/]+)/(?P<schema_id>[^/]+)$", |
| 288 | + }, d, config); err != nil { |
| 289 | + return nil, err |
| 290 | + } |
| 291 | + |
| 292 | + // Replace import id for the resource id |
| 293 | + id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/collections/default_collection/dataStores/{{data_store_id}}/schemas/{{schema_id}}") |
| 294 | + if err != nil { |
| 295 | + return nil, fmt.Errorf("Error constructing id: %s", err) |
| 296 | + } |
| 297 | + d.SetId(id) |
| 298 | + |
| 299 | + return []*schema.ResourceData{d}, nil |
| 300 | +} |
| 301 | + |
| 302 | +func flattenDiscoveryEngineSchemaName(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 303 | + return v |
| 304 | +} |
| 305 | + |
| 306 | +func flattenDiscoveryEngineSchemaJsonSchema(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 307 | + if v == nil { |
| 308 | + return nil |
| 309 | + } |
| 310 | + s, err := structure.NormalizeJsonString(v) |
| 311 | + if err != nil { |
| 312 | + log.Printf("[ERROR] failed to normalize JSON string: %v", err) |
| 313 | + } |
| 314 | + return s |
| 315 | +} |
| 316 | + |
| 317 | +func expandDiscoveryEngineSchemaJsonSchema(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { |
| 318 | + return v, nil |
| 319 | +} |
0 commit comments