Skip to content

Commit eb9a86e

Browse files
SirGitsalottrodge
andcommitted
Add google_site_verification_owner resource (GoogleCloudPlatform#11716)
Co-authored-by: Thomas Rodgers <[email protected]>
1 parent 72adcc7 commit eb9a86e

File tree

4 files changed

+705
-0
lines changed

4 files changed

+705
-0
lines changed

mmv1/third_party/terraform/provider/provider_mmv1_resources.go.tmpl

+1
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ var handwrittenResources = map[string]*schema.Resource{
382382
"google_service_account": resourcemanager.ResourceGoogleServiceAccount(),
383383
"google_service_account_key": resourcemanager.ResourceGoogleServiceAccountKey(),
384384
"google_service_networking_peered_dns_domain": servicenetworking.ResourceGoogleServiceNetworkingPeeredDNSDomain(),
385+
"google_site_verification_owner": siteverification.ResourceSiteVerificationOwner(),
385386
"google_storage_bucket": storage.ResourceStorageBucket(),
386387
"google_storage_bucket_acl": storage.ResourceStorageBucketAcl(),
387388
"google_storage_bucket_object": storage.ResourceStorageBucketObject(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,275 @@
1+
package siteverification
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/http"
7+
"time"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
11+
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
12+
)
13+
14+
func ResourceSiteVerificationOwner() *schema.Resource {
15+
return &schema.Resource{
16+
Create: resourceSiteVerificationOwnerCreate,
17+
Read: resourceSiteVerificationOwnerRead,
18+
Delete: resourceSiteVerificationOwnerDelete,
19+
20+
Importer: &schema.ResourceImporter{
21+
State: resourceSiteVerificationOwnerImport,
22+
},
23+
24+
Timeouts: &schema.ResourceTimeout{
25+
Create: schema.DefaultTimeout(20 * time.Minute),
26+
Delete: schema.DefaultTimeout(20 * time.Minute),
27+
},
28+
29+
Schema: map[string]*schema.Schema{
30+
"email": {
31+
Type: schema.TypeString,
32+
Required: true,
33+
ForceNew: true,
34+
Description: `The email address of the owner.`,
35+
},
36+
"web_resource_id": {
37+
Type: schema.TypeString,
38+
Required: true,
39+
ForceNew: true,
40+
DiffSuppressFunc: tpgresource.CompareSelfLinkOrResourceName,
41+
Description: `The id of the Web Resource to add this owner to, in the form "webResource/<web-resource-id>".`,
42+
},
43+
},
44+
UseJSONNumber: true,
45+
}
46+
}
47+
48+
func resourceSiteVerificationOwnerCreate(d *schema.ResourceData, meta interface{}) error {
49+
email := d.Get("email").(string)
50+
51+
config := meta.(*transport_tpg.Config)
52+
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
53+
if err != nil {
54+
return err
55+
}
56+
57+
log.Printf("[DEBUG] Reading existing WebResource")
58+
59+
url, err := tpgresource.ReplaceVars(d, config, "{{SiteVerificationBasePath}}{{web_resource_id}}")
60+
if err != nil {
61+
return err
62+
}
63+
64+
billingProject := ""
65+
66+
// err == nil indicates that the billing_project value was found
67+
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
68+
billingProject = bp
69+
}
70+
71+
headers := make(http.Header)
72+
obj, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
73+
Config: config,
74+
Method: "GET",
75+
Project: billingProject,
76+
RawURL: url,
77+
UserAgent: userAgent,
78+
Headers: headers,
79+
})
80+
if err != nil {
81+
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("SiteVerificationWebResource %q", d.Id()))
82+
}
83+
84+
log.Printf("[DEBUG] Finished reading WebResource: %#v", obj)
85+
86+
owners, ok := obj["owners"].([]interface{})
87+
if !ok {
88+
return fmt.Errorf("WebResource has no existing owners")
89+
}
90+
found := false
91+
for _, owner := range owners {
92+
if s, ok := owner.(string); ok && s == email {
93+
found = true
94+
}
95+
}
96+
if !found {
97+
owners = append(owners, email)
98+
obj["owners"] = owners
99+
100+
log.Printf("[DEBUG] Creating new Owner: %#v", obj)
101+
102+
headers = make(http.Header)
103+
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
104+
Config: config,
105+
Method: "PUT",
106+
Project: billingProject,
107+
RawURL: url,
108+
UserAgent: userAgent,
109+
Body: obj,
110+
Timeout: d.Timeout(schema.TimeoutCreate),
111+
Headers: headers,
112+
})
113+
if err != nil {
114+
return fmt.Errorf("Error creating Owner: %s", err)
115+
}
116+
117+
log.Printf("[DEBUG] Finished creating Owner %q: %#v", d.Id(), res)
118+
}
119+
120+
// Store the ID now
121+
id, err := tpgresource.ReplaceVars(d, config, "{{web_resource_id}}/{{email}}")
122+
if err != nil {
123+
return fmt.Errorf("Error constructing id: %s", err)
124+
}
125+
d.SetId(id)
126+
127+
return resourceSiteVerificationOwnerRead(d, meta)
128+
}
129+
130+
func resourceSiteVerificationOwnerRead(d *schema.ResourceData, meta interface{}) error {
131+
config := meta.(*transport_tpg.Config)
132+
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
133+
if err != nil {
134+
return err
135+
}
136+
137+
url, err := tpgresource.ReplaceVars(d, config, "{{SiteVerificationBasePath}}{{web_resource_id}}")
138+
if err != nil {
139+
return err
140+
}
141+
142+
billingProject := ""
143+
144+
// err == nil indicates that the billing_project value was found
145+
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
146+
billingProject = bp
147+
}
148+
149+
headers := make(http.Header)
150+
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
151+
Config: config,
152+
Method: "GET",
153+
Project: billingProject,
154+
RawURL: url,
155+
UserAgent: userAgent,
156+
Headers: headers,
157+
})
158+
if err != nil {
159+
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("SiteVerificationOwner %q", d.Id()))
160+
}
161+
162+
owners, ok := res["owners"].([]interface{})
163+
if !ok {
164+
return fmt.Errorf("WebResource has no owners")
165+
}
166+
167+
found := false
168+
email := d.Get("email").(string)
169+
for _, owner := range owners {
170+
if s, ok := owner.(string); ok && s == email {
171+
found = true
172+
}
173+
}
174+
175+
if !found {
176+
// Owner isn't there any more - remove from the state.
177+
log.Printf("[DEBUG] Removing SiteVerificationOwner because it couldn't be matched.")
178+
d.SetId("")
179+
return nil
180+
}
181+
182+
return nil
183+
}
184+
185+
func resourceSiteVerificationOwnerDelete(d *schema.ResourceData, meta interface{}) error {
186+
config := meta.(*transport_tpg.Config)
187+
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
188+
if err != nil {
189+
return err
190+
}
191+
192+
billingProject := ""
193+
194+
url, err := tpgresource.ReplaceVars(d, config, "{{SiteVerificationBasePath}}{{web_resource_id}}")
195+
if err != nil {
196+
return err
197+
}
198+
199+
// err == nil indicates that the billing_project value was found
200+
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
201+
billingProject = bp
202+
}
203+
204+
log.Printf("[DEBUG] Reading existing WebResource")
205+
206+
headers := make(http.Header)
207+
obj, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
208+
Config: config,
209+
Method: "GET",
210+
Project: billingProject,
211+
RawURL: url,
212+
UserAgent: userAgent,
213+
Headers: headers,
214+
})
215+
if err != nil {
216+
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("SiteVerificationWebResource %q", d.Id()))
217+
}
218+
219+
log.Printf("[DEBUG] Finished reading WebResource: %#v", obj)
220+
221+
owners, ok := obj["owners"].([]interface{})
222+
if !ok {
223+
return fmt.Errorf("WebResource has no existing owners")
224+
}
225+
var updatedOwners []interface{}
226+
email := d.Get("email").(string)
227+
for _, owner := range owners {
228+
if s, ok := owner.(string); ok {
229+
if s != email {
230+
updatedOwners = append(updatedOwners, s)
231+
}
232+
}
233+
}
234+
obj["owners"] = updatedOwners
235+
236+
headers = make(http.Header)
237+
238+
log.Printf("[DEBUG] Deleting Owner %q", d.Id())
239+
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
240+
Config: config,
241+
Method: "PUT",
242+
Project: billingProject,
243+
RawURL: url,
244+
UserAgent: userAgent,
245+
Body: obj,
246+
Timeout: d.Timeout(schema.TimeoutDelete),
247+
Headers: headers,
248+
})
249+
if err != nil {
250+
return transport_tpg.HandleNotFoundError(err, d, "Owner")
251+
}
252+
253+
log.Printf("[DEBUG] Finished deleting Owner %q: %#v", d.Id(), res)
254+
return nil
255+
}
256+
257+
func resourceSiteVerificationOwnerImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
258+
config := meta.(*transport_tpg.Config)
259+
if err := tpgresource.ParseImportId([]string{
260+
"^(?P<web_resource_id>webResource/[^/]+)/(?P<email>[^/]+)$",
261+
}, d, config); err != nil {
262+
return nil, err
263+
}
264+
265+
// Replace import id for the resource id
266+
id, err := tpgresource.ReplaceVars(d, config, "{{web_resource_id}}/{{email}}")
267+
if err != nil {
268+
return nil, fmt.Errorf("Error constructing id: %s", err)
269+
}
270+
d.SetId(id)
271+
272+
log.Printf("[DEBUG] Finished importing Owner %q", d.Id())
273+
274+
return []*schema.ResourceData{d}, nil
275+
}

0 commit comments

Comments
 (0)