|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | +// Copyright (c) HashiCorp, Inc. |
| 4 | +// SPDX-License-Identifier: MPL-2.0 |
| 5 | +package siteverification |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "log" |
| 10 | + "net/http" |
| 11 | + "reflect" |
| 12 | + "regexp" |
| 13 | + "time" |
| 14 | + |
| 15 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 16 | + "github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource" |
| 17 | + transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport" |
| 18 | + "github.com/hashicorp/terraform-provider-google-beta/google-beta/verify" |
| 19 | +) |
| 20 | + |
| 21 | +func DataSourceSiteVerificationToken() *schema.Resource { |
| 22 | + return &schema.Resource{ |
| 23 | + Read: dataSourceSiteVerificationTokenRead, |
| 24 | + |
| 25 | + Timeouts: &schema.ResourceTimeout{ |
| 26 | + Read: schema.DefaultTimeout(5 * time.Minute), |
| 27 | + }, |
| 28 | + |
| 29 | + Schema: map[string]*schema.Schema{ |
| 30 | + "identifier": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Required: true, |
| 33 | + ForceNew: true, |
| 34 | + Description: `The site identifier. If the type is set to SITE, the identifier is a URL. If the type is |
| 35 | +set to INET_DOMAIN, the identifier is a domain name.`, |
| 36 | + }, |
| 37 | + "type": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Required: true, |
| 40 | + ForceNew: true, |
| 41 | + ValidateFunc: verify.ValidateEnum([]string{"INET_DOMAIN", "SITE"}), |
| 42 | + Description: `The type of resource to be verified, either a domain or a web site. Possible values: ["INET_DOMAIN", "SITE"]`, |
| 43 | + }, |
| 44 | + "verification_method": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Required: true, |
| 47 | + ForceNew: true, |
| 48 | + ValidateFunc: verify.ValidateEnum([]string{"ANALYTICS", "DNS_CNAME", "DNS_TXT", "FILE", "META", "TAG_MANAGER"}), |
| 49 | + Description: `The verification method for the Site Verification system to use to verify |
| 50 | +this site or domain. Possible values: ["ANALYTICS", "DNS_CNAME", "DNS_TXT", "FILE", "META", "TAG_MANAGER"]`, |
| 51 | + }, |
| 52 | + "token": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Computed: true, |
| 55 | + Description: `The returned token for use in subsequent verification steps.`, |
| 56 | + }, |
| 57 | + }, |
| 58 | + UseJSONNumber: true, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func dataSourceSiteVerificationTokenRead(d *schema.ResourceData, meta interface{}) error { |
| 63 | + config := meta.(*transport_tpg.Config) |
| 64 | + userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + |
| 69 | + obj := make(map[string]interface{}) |
| 70 | + site := make(map[string]interface{}) |
| 71 | + typeProp, err := expandSiteVerificationTokenType(d.Get("type"), d, config) |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } else if v, ok := d.GetOkExists("type"); !tpgresource.IsEmptyValue(reflect.ValueOf(typeProp)) && (ok || !reflect.DeepEqual(v, typeProp)) { |
| 75 | + site["type"] = typeProp |
| 76 | + } |
| 77 | + identifierProp, err := expandSiteVerificationTokenIdentifier(d.Get("identifier"), d, config) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } else if v, ok := d.GetOkExists("identifier"); !tpgresource.IsEmptyValue(reflect.ValueOf(identifierProp)) && (ok || !reflect.DeepEqual(v, identifierProp)) { |
| 81 | + site["identifier"] = identifierProp |
| 82 | + } |
| 83 | + obj["site"] = site |
| 84 | + verification_methodProp, err := expandSiteVerificationTokenVerificationMethod(d.Get("verification_method"), d, config) |
| 85 | + if err != nil { |
| 86 | + return err |
| 87 | + } else if v, ok := d.GetOkExists("verification_method"); !tpgresource.IsEmptyValue(reflect.ValueOf(verification_methodProp)) && (ok || !reflect.DeepEqual(v, verification_methodProp)) { |
| 88 | + obj["verificationMethod"] = verification_methodProp |
| 89 | + } |
| 90 | + |
| 91 | + url, err := tpgresource.ReplaceVars(d, config, "{{SiteVerificationBasePath}}token") |
| 92 | + if err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + |
| 96 | + log.Printf("[DEBUG] Reading Token: %#v", obj) |
| 97 | + billingProject := "" |
| 98 | + |
| 99 | + if parts := regexp.MustCompile(`projects\/([^\/]+)\/`).FindStringSubmatch(url); parts != nil { |
| 100 | + billingProject = parts[1] |
| 101 | + } |
| 102 | + |
| 103 | + // err == nil indicates that the billing_project value was found |
| 104 | + if bp, err := tpgresource.GetBillingProject(d, config); err == nil { |
| 105 | + billingProject = bp |
| 106 | + } |
| 107 | + |
| 108 | + headers := make(http.Header) |
| 109 | + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| 110 | + Config: config, |
| 111 | + Method: "POST", |
| 112 | + Project: billingProject, |
| 113 | + RawURL: url, |
| 114 | + UserAgent: userAgent, |
| 115 | + Body: obj, |
| 116 | + Timeout: d.Timeout(schema.TimeoutCreate), |
| 117 | + Headers: headers, |
| 118 | + }) |
| 119 | + if err != nil { |
| 120 | + return fmt.Errorf("Error reading Token: %s", err) |
| 121 | + } |
| 122 | + |
| 123 | + // Store the ID now |
| 124 | + id, err := tpgresource.ReplaceVars(d, config, "{{identifier}}") |
| 125 | + if err != nil { |
| 126 | + return fmt.Errorf("Error constructing id: %s", err) |
| 127 | + } |
| 128 | + d.SetId(id) |
| 129 | + |
| 130 | + if token, ok := res["token"].(string); ok { |
| 131 | + d.Set("token", token) |
| 132 | + } |
| 133 | + |
| 134 | + log.Printf("[DEBUG] Finished reading Token %q: %#v", d.Id(), res) |
| 135 | + |
| 136 | + return nil |
| 137 | +} |
| 138 | + |
| 139 | +func expandSiteVerificationTokenType(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { |
| 140 | + return v, nil |
| 141 | +} |
| 142 | + |
| 143 | +func expandSiteVerificationTokenIdentifier(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { |
| 144 | + return v, nil |
| 145 | +} |
| 146 | + |
| 147 | +func expandSiteVerificationTokenVerificationMethod(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { |
| 148 | + return v, nil |
| 149 | +} |
0 commit comments