-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add data_source_google_site_verification_token (#10999) (#7704)
[upstream:b33d6de70b36556872c5d0d870116b057878daf2] Signed-off-by: Modular Magician <[email protected]>
- Loading branch information
1 parent
5f85165
commit f20185e
Showing
11 changed files
with
376 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-datasource | ||
`google_site_verification_token` | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
google-beta/services/siteverification/data_source_google_site_verification_token.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
package siteverification | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"reflect" | ||
"regexp" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource" | ||
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport" | ||
"github.com/hashicorp/terraform-provider-google-beta/google-beta/verify" | ||
) | ||
|
||
func DataSourceSiteVerificationToken() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceSiteVerificationTokenRead, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Read: schema.DefaultTimeout(5 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"identifier": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Description: `The site identifier. If the type is set to SITE, the identifier is a URL. If the type is | ||
set to INET_DOMAIN, the identifier is a domain name.`, | ||
}, | ||
"type": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: verify.ValidateEnum([]string{"INET_DOMAIN", "SITE"}), | ||
Description: `The type of resource to be verified, either a domain or a web site. Possible values: ["INET_DOMAIN", "SITE"]`, | ||
}, | ||
"verification_method": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: verify.ValidateEnum([]string{"ANALYTICS", "DNS_CNAME", "DNS_TXT", "FILE", "META", "TAG_MANAGER"}), | ||
Description: `The verification method for the Site Verification system to use to verify | ||
this site or domain. Possible values: ["ANALYTICS", "DNS_CNAME", "DNS_TXT", "FILE", "META", "TAG_MANAGER"]`, | ||
}, | ||
"token": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `The returned token for use in subsequent verification steps.`, | ||
}, | ||
}, | ||
UseJSONNumber: true, | ||
} | ||
} | ||
|
||
func dataSourceSiteVerificationTokenRead(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*transport_tpg.Config) | ||
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
obj := make(map[string]interface{}) | ||
site := make(map[string]interface{}) | ||
typeProp, err := expandSiteVerificationTokenType(d.Get("type"), d, config) | ||
if err != nil { | ||
return err | ||
} else if v, ok := d.GetOkExists("type"); !tpgresource.IsEmptyValue(reflect.ValueOf(typeProp)) && (ok || !reflect.DeepEqual(v, typeProp)) { | ||
site["type"] = typeProp | ||
} | ||
identifierProp, err := expandSiteVerificationTokenIdentifier(d.Get("identifier"), d, config) | ||
if err != nil { | ||
return err | ||
} else if v, ok := d.GetOkExists("identifier"); !tpgresource.IsEmptyValue(reflect.ValueOf(identifierProp)) && (ok || !reflect.DeepEqual(v, identifierProp)) { | ||
site["identifier"] = identifierProp | ||
} | ||
obj["site"] = site | ||
verification_methodProp, err := expandSiteVerificationTokenVerificationMethod(d.Get("verification_method"), d, config) | ||
if err != nil { | ||
return err | ||
} else if v, ok := d.GetOkExists("verification_method"); !tpgresource.IsEmptyValue(reflect.ValueOf(verification_methodProp)) && (ok || !reflect.DeepEqual(v, verification_methodProp)) { | ||
obj["verificationMethod"] = verification_methodProp | ||
} | ||
|
||
url, err := tpgresource.ReplaceVars(d, config, "{{SiteVerificationBasePath}}token") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("[DEBUG] Reading Token: %#v", obj) | ||
billingProject := "" | ||
|
||
if parts := regexp.MustCompile(`projects\/([^\/]+)\/`).FindStringSubmatch(url); parts != nil { | ||
billingProject = parts[1] | ||
} | ||
|
||
// err == nil indicates that the billing_project value was found | ||
if bp, err := tpgresource.GetBillingProject(d, config); err == nil { | ||
billingProject = bp | ||
} | ||
|
||
headers := make(http.Header) | ||
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ | ||
Config: config, | ||
Method: "POST", | ||
Project: billingProject, | ||
RawURL: url, | ||
UserAgent: userAgent, | ||
Body: obj, | ||
Timeout: d.Timeout(schema.TimeoutCreate), | ||
Headers: headers, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("Error reading Token: %s", err) | ||
} | ||
|
||
// Store the ID now | ||
id, err := tpgresource.ReplaceVars(d, config, "{{identifier}}") | ||
if err != nil { | ||
return fmt.Errorf("Error constructing id: %s", err) | ||
} | ||
d.SetId(id) | ||
|
||
if token, ok := res["token"].(string); ok { | ||
d.Set("token", token) | ||
} | ||
|
||
log.Printf("[DEBUG] Finished reading Token %q: %#v", d.Id(), res) | ||
|
||
return nil | ||
} | ||
|
||
func expandSiteVerificationTokenType(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { | ||
return v, nil | ||
} | ||
|
||
func expandSiteVerificationTokenIdentifier(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { | ||
return v, nil | ||
} | ||
|
||
func expandSiteVerificationTokenVerificationMethod(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { | ||
return v, nil | ||
} |
105 changes: 105 additions & 0 deletions
105
google-beta/services/siteverification/data_source_google_site_verification_token_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
package siteverification_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest" | ||
) | ||
|
||
func TestAccSiteVerificationToken_siteverificationTokenSite(t *testing.T) { | ||
t.Parallel() | ||
|
||
context := map[string]interface{}{ | ||
"site": "https://www.example.com", | ||
} | ||
|
||
acctest.VcrTest(t, resource.TestCase{ | ||
PreCheck: func() { acctest.AccTestPreCheck(t) }, | ||
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccSiteVerificationToken_siteverificationTokenSite(context), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("data.google_site_verification_token.site_meta", "token"), | ||
resource.TestCheckResourceAttr("data.google_site_verification_token.site_meta", "type", "SITE"), | ||
resource.TestCheckResourceAttr("data.google_site_verification_token.site_meta", "identifier", context["site"].(string)), | ||
resource.TestCheckResourceAttr("data.google_site_verification_token.site_meta", "verification_method", "META"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccSiteVerificationToken_siteverificationTokenSite(context map[string]interface{}) string { | ||
return acctest.Nprintf(` | ||
provider "google" { | ||
alias = "scoped" | ||
user_project_override = true | ||
scopes = [ | ||
"https://www.googleapis.com/auth/siteverification", | ||
"https://www.googleapis.com/auth/cloud-platform", | ||
"https://www.googleapis.com/auth/userinfo.email", | ||
] | ||
} | ||
data "google_site_verification_token" "site_meta" { | ||
provider = google.scoped | ||
type = "SITE" | ||
identifier = "%{site}" | ||
verification_method = "META" | ||
} | ||
`, context) | ||
} | ||
|
||
func TestAccSiteVerificationToken_siteverificationTokenDomain(t *testing.T) { | ||
t.Parallel() | ||
|
||
context := map[string]interface{}{ | ||
"domain": "www.example.com", | ||
} | ||
|
||
acctest.VcrTest(t, resource.TestCase{ | ||
PreCheck: func() { acctest.AccTestPreCheck(t) }, | ||
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), | ||
ExternalProviders: map[string]resource.ExternalProvider{ | ||
"time": {}, | ||
}, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccSiteVerificationToken_siteverificationTokenDomain(context), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("data.google_site_verification_token.dns_text", "token"), | ||
resource.TestCheckResourceAttr("data.google_site_verification_token.dns_text", "type", "INET_DOMAIN"), | ||
resource.TestCheckResourceAttr("data.google_site_verification_token.dns_text", "identifier", context["domain"].(string)), | ||
resource.TestCheckResourceAttr("data.google_site_verification_token.dns_text", "verification_method", "DNS_TXT"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccSiteVerificationToken_siteverificationTokenDomain(context map[string]interface{}) string { | ||
return acctest.Nprintf(` | ||
provider "google" { | ||
alias = "scoped" | ||
user_project_override = true | ||
scopes = [ | ||
"https://www.googleapis.com/auth/siteverification", | ||
"https://www.googleapis.com/auth/cloud-platform", | ||
"https://www.googleapis.com/auth/userinfo.email", | ||
] | ||
} | ||
data "google_site_verification_token" "dns_text" { | ||
provider = google.scoped | ||
type = "INET_DOMAIN" | ||
identifier = "%{domain}" | ||
verification_method = "DNS_TXT" | ||
} | ||
`, context) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.