Skip to content

Commit

Permalink
Compute public advertised prefixes (#7682) (#5476)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Apr 13, 2023
1 parent 80e1205 commit 3e70289
Show file tree
Hide file tree
Showing 10 changed files with 1,351 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changelog/7682.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
```release-note:new-resource
`google_compute_public_advertised_prefix`
```
```release-note:new-resource
`google_compute_public_delegated_prefix`
```
6 changes: 4 additions & 2 deletions google-beta/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -788,9 +788,9 @@ func Provider() *schema.Provider {
return provider
}

// Generated resources: 321
// Generated resources: 323
// Generated IAM resources: 213
// Total generated resources: 534
// Total generated resources: 536
func ResourceMap() map[string]*schema.Resource {
resourceMap, _ := ResourceMapWithErrors()
return resourceMap
Expand Down Expand Up @@ -994,6 +994,8 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) {
"google_compute_organization_security_policy_rule": ResourceComputeOrganizationSecurityPolicyRule(),
"google_compute_packet_mirroring": ResourceComputePacketMirroring(),
"google_compute_per_instance_config": ResourceComputePerInstanceConfig(),
"google_compute_public_advertised_prefix": ResourceComputePublicAdvertisedPrefix(),
"google_compute_public_delegated_prefix": ResourceComputePublicDelegatedPrefix(),
"google_compute_region_autoscaler": ResourceComputeRegionAutoscaler(),
"google_compute_region_backend_service": ResourceComputeRegionBackendService(),
"google_compute_region_backend_service_iam_binding": ResourceIamBinding(ComputeRegionBackendServiceIamSchema, ComputeRegionBackendServiceIamUpdaterProducer, ComputeRegionBackendServiceIdParseFunc),
Expand Down
11 changes: 11 additions & 0 deletions google-beta/provider_test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ var masterBillingAccountEnvVars = []string{
"GOOGLE_MASTER_BILLING_ACCOUNT",
}

// This value is the description used for test PublicAdvertisedPrefix setup to avoid required DNS
// setup. This is only used during integration tests and would be invalid to surface to users
var papDescriptionEnvVars = []string{
"GOOGLE_PUBLIC_AVERTISED_PREFIX_DESCRIPTION",
}

func init() {
configs = make(map[string]*Config)
fwProviders = make(map[string]*frameworkTestProvider)
Expand Down Expand Up @@ -258,6 +264,11 @@ func GetTestServiceAccountFromEnv(t *testing.T) string {
return MultiEnvSearch(serviceAccountEnvVars)
}

func GetTestPublicAdvertisedPrefixDescriptionFromEnv(t *testing.T) string {
SkipIfEnvNotSet(t, papDescriptionEnvVars...)
return MultiEnvSearch(papDescriptionEnvVars)
}

// Some tests fail during VCR. One common case is race conditions when creating resources.
// If a test config adds two fine-grained resources with the same parent it is undefined
// which will be created first, causing VCR to fail ~50% of the time
Expand Down
313 changes: 313 additions & 0 deletions google-beta/resource_compute_public_advertised_prefix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,313 @@
// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** Type: MMv1 ***
//
// ----------------------------------------------------------------------------
//
// This file is automatically generated by Magic Modules and manual
// changes will be clobbered when the file is regenerated.
//
// Please read more about how to change this file in
// .github/CONTRIBUTING.md.
//
// ----------------------------------------------------------------------------

package google

import (
"fmt"
"log"
"reflect"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func ResourceComputePublicAdvertisedPrefix() *schema.Resource {
return &schema.Resource{
Create: resourceComputePublicAdvertisedPrefixCreate,
Read: resourceComputePublicAdvertisedPrefixRead,
Delete: resourceComputePublicAdvertisedPrefixDelete,

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

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

Schema: map[string]*schema.Schema{
"dns_verification_ip": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The IPv4 address to be used for reverse DNS verification.`,
},
"ip_cidr_range": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The IPv4 address range, in CIDR format, represented by this public advertised prefix.`,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `Name of the resource. The name must be 1-63 characters long, and
comply with RFC1035. Specifically, the name must be 1-63 characters
long and match the regular expression '[a-z]([-a-z0-9]*[a-z0-9])?'
which means the first character must be a lowercase letter, and all
following characters must be a dash, lowercase letter, or digit,
except the last character, which cannot be a dash.`,
},
"description": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `An optional description of this resource.`,
},
"project": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"self_link": {
Type: schema.TypeString,
Computed: true,
},
},
UseJSONNumber: true,
}
}

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

obj := make(map[string]interface{})
descriptionProp, err := expandComputePublicAdvertisedPrefixDescription(d.Get("description"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("description"); !isEmptyValue(reflect.ValueOf(descriptionProp)) && (ok || !reflect.DeepEqual(v, descriptionProp)) {
obj["description"] = descriptionProp
}
nameProp, err := expandComputePublicAdvertisedPrefixName(d.Get("name"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("name"); !isEmptyValue(reflect.ValueOf(nameProp)) && (ok || !reflect.DeepEqual(v, nameProp)) {
obj["name"] = nameProp
}
dnsVerificationIpProp, err := expandComputePublicAdvertisedPrefixDnsVerificationIp(d.Get("dns_verification_ip"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("dns_verification_ip"); !isEmptyValue(reflect.ValueOf(dnsVerificationIpProp)) && (ok || !reflect.DeepEqual(v, dnsVerificationIpProp)) {
obj["dnsVerificationIp"] = dnsVerificationIpProp
}
ipCidrRangeProp, err := expandComputePublicAdvertisedPrefixIpCidrRange(d.Get("ip_cidr_range"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("ip_cidr_range"); !isEmptyValue(reflect.ValueOf(ipCidrRangeProp)) && (ok || !reflect.DeepEqual(v, ipCidrRangeProp)) {
obj["ipCidrRange"] = ipCidrRangeProp
}

url, err := ReplaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/global/publicAdvertisedPrefixes")
if err != nil {
return err
}

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

project, err := getProject(d, config)
if err != nil {
return fmt.Errorf("Error fetching project for PublicAdvertisedPrefix: %s", err)
}
billingProject = project

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

res, err := SendRequestWithTimeout(config, "POST", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutCreate))
if err != nil {
return fmt.Errorf("Error creating PublicAdvertisedPrefix: %s", err)
}

// Store the ID now
id, err := ReplaceVars(d, config, "projects/{{project}}/global/publicAdvertisedPrefixes/{{name}}")
if err != nil {
return fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)

err = ComputeOperationWaitTime(
config, res, project, "Creating PublicAdvertisedPrefix", userAgent,
d.Timeout(schema.TimeoutCreate))

if err != nil {
// The resource didn't actually create
d.SetId("")
return fmt.Errorf("Error waiting to create PublicAdvertisedPrefix: %s", err)
}

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

return resourceComputePublicAdvertisedPrefixRead(d, meta)
}

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

url, err := ReplaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/global/publicAdvertisedPrefixes/{{name}}")
if err != nil {
return err
}

billingProject := ""

project, err := getProject(d, config)
if err != nil {
return fmt.Errorf("Error fetching project for PublicAdvertisedPrefix: %s", err)
}
billingProject = project

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

res, err := SendRequest(config, "GET", billingProject, url, userAgent, nil)
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("ComputePublicAdvertisedPrefix %q", d.Id()))
}

if err := d.Set("project", project); err != nil {
return fmt.Errorf("Error reading PublicAdvertisedPrefix: %s", err)
}

if err := d.Set("description", flattenComputePublicAdvertisedPrefixDescription(res["description"], d, config)); err != nil {
return fmt.Errorf("Error reading PublicAdvertisedPrefix: %s", err)
}
if err := d.Set("name", flattenComputePublicAdvertisedPrefixName(res["name"], d, config)); err != nil {
return fmt.Errorf("Error reading PublicAdvertisedPrefix: %s", err)
}
if err := d.Set("dns_verification_ip", flattenComputePublicAdvertisedPrefixDnsVerificationIp(res["dnsVerificationIp"], d, config)); err != nil {
return fmt.Errorf("Error reading PublicAdvertisedPrefix: %s", err)
}
if err := d.Set("ip_cidr_range", flattenComputePublicAdvertisedPrefixIpCidrRange(res["ipCidrRange"], d, config)); err != nil {
return fmt.Errorf("Error reading PublicAdvertisedPrefix: %s", err)
}
if err := d.Set("self_link", ConvertSelfLinkToV1(res["selfLink"].(string))); err != nil {
return fmt.Errorf("Error reading PublicAdvertisedPrefix: %s", err)
}

return nil
}

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

billingProject := ""

project, err := getProject(d, config)
if err != nil {
return fmt.Errorf("Error fetching project for PublicAdvertisedPrefix: %s", err)
}
billingProject = project

url, err := ReplaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/global/publicAdvertisedPrefixes/{{name}}")
if err != nil {
return err
}

var obj map[string]interface{}
log.Printf("[DEBUG] Deleting PublicAdvertisedPrefix %q", d.Id())

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

res, err := SendRequestWithTimeout(config, "DELETE", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutDelete))
if err != nil {
return handleNotFoundError(err, d, "PublicAdvertisedPrefix")
}

err = ComputeOperationWaitTime(
config, res, project, "Deleting PublicAdvertisedPrefix", userAgent,
d.Timeout(schema.TimeoutDelete))

if err != nil {
return err
}

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

func resourceComputePublicAdvertisedPrefixImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*Config)
if err := ParseImportId([]string{
"projects/(?P<project>[^/]+)/global/publicAdvertisedPrefixes/(?P<name>[^/]+)",
"(?P<project>[^/]+)/(?P<name>[^/]+)",
"(?P<name>[^/]+)",
}, d, config); err != nil {
return nil, err
}

// Replace import id for the resource id
id, err := ReplaceVars(d, config, "projects/{{project}}/global/publicAdvertisedPrefixes/{{name}}")
if err != nil {
return nil, fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)

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

func flattenComputePublicAdvertisedPrefixDescription(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func flattenComputePublicAdvertisedPrefixName(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func flattenComputePublicAdvertisedPrefixDnsVerificationIp(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func flattenComputePublicAdvertisedPrefixIpCidrRange(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func expandComputePublicAdvertisedPrefixDescription(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputePublicAdvertisedPrefixName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputePublicAdvertisedPrefixDnsVerificationIp(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputePublicAdvertisedPrefixIpCidrRange(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Loading

0 comments on commit 3e70289

Please sign in to comment.