Skip to content

Commit

Permalink
Add google_site_verification_owner resource (#11716)
Browse files Browse the repository at this point in the history
Co-authored-by: Thomas Rodgers <[email protected]>
  • Loading branch information
SirGitsalot and trodge authored Sep 26, 2024
1 parent 5422306 commit 9702300
Show file tree
Hide file tree
Showing 4 changed files with 705 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ var handwrittenResources = map[string]*schema.Resource{
"google_service_account": resourcemanager.ResourceGoogleServiceAccount(),
"google_service_account_key": resourcemanager.ResourceGoogleServiceAccountKey(),
"google_service_networking_peered_dns_domain": servicenetworking.ResourceGoogleServiceNetworkingPeeredDNSDomain(),
"google_site_verification_owner": siteverification.ResourceSiteVerificationOwner(),
"google_storage_bucket": storage.ResourceStorageBucket(),
"google_storage_bucket_acl": storage.ResourceStorageBucketAcl(),
"google_storage_bucket_object": storage.ResourceStorageBucketObject(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,275 @@
package siteverification

import (
"fmt"
"log"
"net/http"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
)

func ResourceSiteVerificationOwner() *schema.Resource {
return &schema.Resource{
Create: resourceSiteVerificationOwnerCreate,
Read: resourceSiteVerificationOwnerRead,
Delete: resourceSiteVerificationOwnerDelete,

Importer: &schema.ResourceImporter{
State: resourceSiteVerificationOwnerImport,
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(20 * time.Minute),
Delete: schema.DefaultTimeout(20 * time.Minute),
},

Schema: map[string]*schema.Schema{
"email": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The email address of the owner.`,
},
"web_resource_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: tpgresource.CompareSelfLinkOrResourceName,
Description: `The id of the Web Resource to add this owner to, in the form "webResource/<web-resource-id>".`,
},
},
UseJSONNumber: true,
}
}

func resourceSiteVerificationOwnerCreate(d *schema.ResourceData, meta interface{}) error {
email := d.Get("email").(string)

config := meta.(*transport_tpg.Config)
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
return err
}

log.Printf("[DEBUG] Reading existing WebResource")

url, err := tpgresource.ReplaceVars(d, config, "{{SiteVerificationBasePath}}{{web_resource_id}}")
if err != nil {
return err
}

billingProject := ""

// 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)
obj, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "GET",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
Headers: headers,
})
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("SiteVerificationWebResource %q", d.Id()))
}

log.Printf("[DEBUG] Finished reading WebResource: %#v", obj)

owners, ok := obj["owners"].([]interface{})
if !ok {
return fmt.Errorf("WebResource has no existing owners")
}
found := false
for _, owner := range owners {
if s, ok := owner.(string); ok && s == email {
found = true
}
}
if !found {
owners = append(owners, email)
obj["owners"] = owners

log.Printf("[DEBUG] Creating new Owner: %#v", obj)

headers = make(http.Header)
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "PUT",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
Body: obj,
Timeout: d.Timeout(schema.TimeoutCreate),
Headers: headers,
})
if err != nil {
return fmt.Errorf("Error creating Owner: %s", err)
}

log.Printf("[DEBUG] Finished creating Owner %q: %#v", d.Id(), res)
}

// Store the ID now
id, err := tpgresource.ReplaceVars(d, config, "{{web_resource_id}}/{{email}}")
if err != nil {
return fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)

return resourceSiteVerificationOwnerRead(d, meta)
}

func resourceSiteVerificationOwnerRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
return err
}

url, err := tpgresource.ReplaceVars(d, config, "{{SiteVerificationBasePath}}{{web_resource_id}}")
if err != nil {
return err
}

billingProject := ""

// 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: "GET",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
Headers: headers,
})
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("SiteVerificationOwner %q", d.Id()))
}

owners, ok := res["owners"].([]interface{})
if !ok {
return fmt.Errorf("WebResource has no owners")
}

found := false
email := d.Get("email").(string)
for _, owner := range owners {
if s, ok := owner.(string); ok && s == email {
found = true
}
}

if !found {
// Owner isn't there any more - remove from the state.
log.Printf("[DEBUG] Removing SiteVerificationOwner because it couldn't be matched.")
d.SetId("")
return nil
}

return nil
}

func resourceSiteVerificationOwnerDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
return err
}

billingProject := ""

url, err := tpgresource.ReplaceVars(d, config, "{{SiteVerificationBasePath}}{{web_resource_id}}")
if err != nil {
return err
}

// err == nil indicates that the billing_project value was found
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
billingProject = bp
}

log.Printf("[DEBUG] Reading existing WebResource")

headers := make(http.Header)
obj, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "GET",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
Headers: headers,
})
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("SiteVerificationWebResource %q", d.Id()))
}

log.Printf("[DEBUG] Finished reading WebResource: %#v", obj)

owners, ok := obj["owners"].([]interface{})
if !ok {
return fmt.Errorf("WebResource has no existing owners")
}
var updatedOwners []interface{}
email := d.Get("email").(string)
for _, owner := range owners {
if s, ok := owner.(string); ok {
if s != email {
updatedOwners = append(updatedOwners, s)
}
}
}
obj["owners"] = updatedOwners

headers = make(http.Header)

log.Printf("[DEBUG] Deleting Owner %q", d.Id())
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "PUT",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
Body: obj,
Timeout: d.Timeout(schema.TimeoutDelete),
Headers: headers,
})
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, "Owner")
}

log.Printf("[DEBUG] Finished deleting Owner %q: %#v", d.Id(), res)
return nil
}

func resourceSiteVerificationOwnerImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*transport_tpg.Config)
if err := tpgresource.ParseImportId([]string{
"^(?P<web_resource_id>webResource/[^/]+)/(?P<email>[^/]+)$",
}, d, config); err != nil {
return nil, err
}

// Replace import id for the resource id
id, err := tpgresource.ReplaceVars(d, config, "{{web_resource_id}}/{{email}}")
if err != nil {
return nil, fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)

log.Printf("[DEBUG] Finished importing Owner %q", d.Id())

return []*schema.ResourceData{d}, nil
}
Loading

0 comments on commit 9702300

Please sign in to comment.