|
| 1 | +// ---------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// *** AUTO GENERATED CODE *** Type: MMv1 *** |
| 4 | +// |
| 5 | +// ---------------------------------------------------------------------------- |
| 6 | +// |
| 7 | +// This file is automatically generated by Magic Modules and manual |
| 8 | +// changes will be clobbered when the file is regenerated. |
| 9 | +// |
| 10 | +// Please read more about how to change this file in |
| 11 | +// .github/CONTRIBUTING.md. |
| 12 | +// |
| 13 | +// ---------------------------------------------------------------------------- |
| 14 | + |
| 15 | +package google |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "log" |
| 20 | + "reflect" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 24 | +) |
| 25 | + |
| 26 | +func ResourceComputePublicAdvertisedPrefix() *schema.Resource { |
| 27 | + return &schema.Resource{ |
| 28 | + Create: resourceComputePublicAdvertisedPrefixCreate, |
| 29 | + Read: resourceComputePublicAdvertisedPrefixRead, |
| 30 | + Delete: resourceComputePublicAdvertisedPrefixDelete, |
| 31 | + |
| 32 | + Importer: &schema.ResourceImporter{ |
| 33 | + State: resourceComputePublicAdvertisedPrefixImport, |
| 34 | + }, |
| 35 | + |
| 36 | + Timeouts: &schema.ResourceTimeout{ |
| 37 | + Create: schema.DefaultTimeout(20 * time.Minute), |
| 38 | + Delete: schema.DefaultTimeout(20 * time.Minute), |
| 39 | + }, |
| 40 | + |
| 41 | + Schema: map[string]*schema.Schema{ |
| 42 | + "dns_verification_ip": { |
| 43 | + Type: schema.TypeString, |
| 44 | + Required: true, |
| 45 | + ForceNew: true, |
| 46 | + Description: `The IPv4 address to be used for reverse DNS verification.`, |
| 47 | + }, |
| 48 | + "ip_cidr_range": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Required: true, |
| 51 | + ForceNew: true, |
| 52 | + Description: `The IPv4 address range, in CIDR format, represented by this public advertised prefix.`, |
| 53 | + }, |
| 54 | + "name": { |
| 55 | + Type: schema.TypeString, |
| 56 | + Required: true, |
| 57 | + ForceNew: true, |
| 58 | + Description: `Name of the resource. The name must be 1-63 characters long, and |
| 59 | +comply with RFC1035. Specifically, the name must be 1-63 characters |
| 60 | +long and match the regular expression '[a-z]([-a-z0-9]*[a-z0-9])?' |
| 61 | +which means the first character must be a lowercase letter, and all |
| 62 | +following characters must be a dash, lowercase letter, or digit, |
| 63 | +except the last character, which cannot be a dash.`, |
| 64 | + }, |
| 65 | + "description": { |
| 66 | + Type: schema.TypeString, |
| 67 | + Optional: true, |
| 68 | + ForceNew: true, |
| 69 | + Description: `An optional description of this resource.`, |
| 70 | + }, |
| 71 | + "project": { |
| 72 | + Type: schema.TypeString, |
| 73 | + Optional: true, |
| 74 | + Computed: true, |
| 75 | + ForceNew: true, |
| 76 | + }, |
| 77 | + "self_link": { |
| 78 | + Type: schema.TypeString, |
| 79 | + Computed: true, |
| 80 | + }, |
| 81 | + }, |
| 82 | + UseJSONNumber: true, |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func resourceComputePublicAdvertisedPrefixCreate(d *schema.ResourceData, meta interface{}) error { |
| 87 | + config := meta.(*Config) |
| 88 | + userAgent, err := generateUserAgentString(d, config.UserAgent) |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + |
| 93 | + obj := make(map[string]interface{}) |
| 94 | + descriptionProp, err := expandComputePublicAdvertisedPrefixDescription(d.Get("description"), d, config) |
| 95 | + if err != nil { |
| 96 | + return err |
| 97 | + } else if v, ok := d.GetOkExists("description"); !isEmptyValue(reflect.ValueOf(descriptionProp)) && (ok || !reflect.DeepEqual(v, descriptionProp)) { |
| 98 | + obj["description"] = descriptionProp |
| 99 | + } |
| 100 | + nameProp, err := expandComputePublicAdvertisedPrefixName(d.Get("name"), d, config) |
| 101 | + if err != nil { |
| 102 | + return err |
| 103 | + } else if v, ok := d.GetOkExists("name"); !isEmptyValue(reflect.ValueOf(nameProp)) && (ok || !reflect.DeepEqual(v, nameProp)) { |
| 104 | + obj["name"] = nameProp |
| 105 | + } |
| 106 | + dnsVerificationIpProp, err := expandComputePublicAdvertisedPrefixDnsVerificationIp(d.Get("dns_verification_ip"), d, config) |
| 107 | + if err != nil { |
| 108 | + return err |
| 109 | + } else if v, ok := d.GetOkExists("dns_verification_ip"); !isEmptyValue(reflect.ValueOf(dnsVerificationIpProp)) && (ok || !reflect.DeepEqual(v, dnsVerificationIpProp)) { |
| 110 | + obj["dnsVerificationIp"] = dnsVerificationIpProp |
| 111 | + } |
| 112 | + ipCidrRangeProp, err := expandComputePublicAdvertisedPrefixIpCidrRange(d.Get("ip_cidr_range"), d, config) |
| 113 | + if err != nil { |
| 114 | + return err |
| 115 | + } else if v, ok := d.GetOkExists("ip_cidr_range"); !isEmptyValue(reflect.ValueOf(ipCidrRangeProp)) && (ok || !reflect.DeepEqual(v, ipCidrRangeProp)) { |
| 116 | + obj["ipCidrRange"] = ipCidrRangeProp |
| 117 | + } |
| 118 | + |
| 119 | + url, err := ReplaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/global/publicAdvertisedPrefixes") |
| 120 | + if err != nil { |
| 121 | + return err |
| 122 | + } |
| 123 | + |
| 124 | + log.Printf("[DEBUG] Creating new PublicAdvertisedPrefix: %#v", obj) |
| 125 | + billingProject := "" |
| 126 | + |
| 127 | + project, err := getProject(d, config) |
| 128 | + if err != nil { |
| 129 | + return fmt.Errorf("Error fetching project for PublicAdvertisedPrefix: %s", err) |
| 130 | + } |
| 131 | + billingProject = project |
| 132 | + |
| 133 | + // err == nil indicates that the billing_project value was found |
| 134 | + if bp, err := getBillingProject(d, config); err == nil { |
| 135 | + billingProject = bp |
| 136 | + } |
| 137 | + |
| 138 | + res, err := SendRequestWithTimeout(config, "POST", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutCreate)) |
| 139 | + if err != nil { |
| 140 | + return fmt.Errorf("Error creating PublicAdvertisedPrefix: %s", err) |
| 141 | + } |
| 142 | + |
| 143 | + // Store the ID now |
| 144 | + id, err := ReplaceVars(d, config, "projects/{{project}}/global/publicAdvertisedPrefixes/{{name}}") |
| 145 | + if err != nil { |
| 146 | + return fmt.Errorf("Error constructing id: %s", err) |
| 147 | + } |
| 148 | + d.SetId(id) |
| 149 | + |
| 150 | + err = ComputeOperationWaitTime( |
| 151 | + config, res, project, "Creating PublicAdvertisedPrefix", userAgent, |
| 152 | + d.Timeout(schema.TimeoutCreate)) |
| 153 | + |
| 154 | + if err != nil { |
| 155 | + // The resource didn't actually create |
| 156 | + d.SetId("") |
| 157 | + return fmt.Errorf("Error waiting to create PublicAdvertisedPrefix: %s", err) |
| 158 | + } |
| 159 | + |
| 160 | + log.Printf("[DEBUG] Finished creating PublicAdvertisedPrefix %q: %#v", d.Id(), res) |
| 161 | + |
| 162 | + return resourceComputePublicAdvertisedPrefixRead(d, meta) |
| 163 | +} |
| 164 | + |
| 165 | +func resourceComputePublicAdvertisedPrefixRead(d *schema.ResourceData, meta interface{}) error { |
| 166 | + config := meta.(*Config) |
| 167 | + userAgent, err := generateUserAgentString(d, config.UserAgent) |
| 168 | + if err != nil { |
| 169 | + return err |
| 170 | + } |
| 171 | + |
| 172 | + url, err := ReplaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/global/publicAdvertisedPrefixes/{{name}}") |
| 173 | + if err != nil { |
| 174 | + return err |
| 175 | + } |
| 176 | + |
| 177 | + billingProject := "" |
| 178 | + |
| 179 | + project, err := getProject(d, config) |
| 180 | + if err != nil { |
| 181 | + return fmt.Errorf("Error fetching project for PublicAdvertisedPrefix: %s", err) |
| 182 | + } |
| 183 | + billingProject = project |
| 184 | + |
| 185 | + // err == nil indicates that the billing_project value was found |
| 186 | + if bp, err := getBillingProject(d, config); err == nil { |
| 187 | + billingProject = bp |
| 188 | + } |
| 189 | + |
| 190 | + res, err := SendRequest(config, "GET", billingProject, url, userAgent, nil) |
| 191 | + if err != nil { |
| 192 | + return handleNotFoundError(err, d, fmt.Sprintf("ComputePublicAdvertisedPrefix %q", d.Id())) |
| 193 | + } |
| 194 | + |
| 195 | + if err := d.Set("project", project); err != nil { |
| 196 | + return fmt.Errorf("Error reading PublicAdvertisedPrefix: %s", err) |
| 197 | + } |
| 198 | + |
| 199 | + if err := d.Set("description", flattenComputePublicAdvertisedPrefixDescription(res["description"], d, config)); err != nil { |
| 200 | + return fmt.Errorf("Error reading PublicAdvertisedPrefix: %s", err) |
| 201 | + } |
| 202 | + if err := d.Set("name", flattenComputePublicAdvertisedPrefixName(res["name"], d, config)); err != nil { |
| 203 | + return fmt.Errorf("Error reading PublicAdvertisedPrefix: %s", err) |
| 204 | + } |
| 205 | + if err := d.Set("dns_verification_ip", flattenComputePublicAdvertisedPrefixDnsVerificationIp(res["dnsVerificationIp"], d, config)); err != nil { |
| 206 | + return fmt.Errorf("Error reading PublicAdvertisedPrefix: %s", err) |
| 207 | + } |
| 208 | + if err := d.Set("ip_cidr_range", flattenComputePublicAdvertisedPrefixIpCidrRange(res["ipCidrRange"], d, config)); err != nil { |
| 209 | + return fmt.Errorf("Error reading PublicAdvertisedPrefix: %s", err) |
| 210 | + } |
| 211 | + if err := d.Set("self_link", ConvertSelfLinkToV1(res["selfLink"].(string))); err != nil { |
| 212 | + return fmt.Errorf("Error reading PublicAdvertisedPrefix: %s", err) |
| 213 | + } |
| 214 | + |
| 215 | + return nil |
| 216 | +} |
| 217 | + |
| 218 | +func resourceComputePublicAdvertisedPrefixDelete(d *schema.ResourceData, meta interface{}) error { |
| 219 | + config := meta.(*Config) |
| 220 | + userAgent, err := generateUserAgentString(d, config.UserAgent) |
| 221 | + if err != nil { |
| 222 | + return err |
| 223 | + } |
| 224 | + |
| 225 | + billingProject := "" |
| 226 | + |
| 227 | + project, err := getProject(d, config) |
| 228 | + if err != nil { |
| 229 | + return fmt.Errorf("Error fetching project for PublicAdvertisedPrefix: %s", err) |
| 230 | + } |
| 231 | + billingProject = project |
| 232 | + |
| 233 | + url, err := ReplaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/global/publicAdvertisedPrefixes/{{name}}") |
| 234 | + if err != nil { |
| 235 | + return err |
| 236 | + } |
| 237 | + |
| 238 | + var obj map[string]interface{} |
| 239 | + log.Printf("[DEBUG] Deleting PublicAdvertisedPrefix %q", d.Id()) |
| 240 | + |
| 241 | + // err == nil indicates that the billing_project value was found |
| 242 | + if bp, err := getBillingProject(d, config); err == nil { |
| 243 | + billingProject = bp |
| 244 | + } |
| 245 | + |
| 246 | + res, err := SendRequestWithTimeout(config, "DELETE", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutDelete)) |
| 247 | + if err != nil { |
| 248 | + return handleNotFoundError(err, d, "PublicAdvertisedPrefix") |
| 249 | + } |
| 250 | + |
| 251 | + err = ComputeOperationWaitTime( |
| 252 | + config, res, project, "Deleting PublicAdvertisedPrefix", userAgent, |
| 253 | + d.Timeout(schema.TimeoutDelete)) |
| 254 | + |
| 255 | + if err != nil { |
| 256 | + return err |
| 257 | + } |
| 258 | + |
| 259 | + log.Printf("[DEBUG] Finished deleting PublicAdvertisedPrefix %q: %#v", d.Id(), res) |
| 260 | + return nil |
| 261 | +} |
| 262 | + |
| 263 | +func resourceComputePublicAdvertisedPrefixImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { |
| 264 | + config := meta.(*Config) |
| 265 | + if err := ParseImportId([]string{ |
| 266 | + "projects/(?P<project>[^/]+)/global/publicAdvertisedPrefixes/(?P<name>[^/]+)", |
| 267 | + "(?P<project>[^/]+)/(?P<name>[^/]+)", |
| 268 | + "(?P<name>[^/]+)", |
| 269 | + }, d, config); err != nil { |
| 270 | + return nil, err |
| 271 | + } |
| 272 | + |
| 273 | + // Replace import id for the resource id |
| 274 | + id, err := ReplaceVars(d, config, "projects/{{project}}/global/publicAdvertisedPrefixes/{{name}}") |
| 275 | + if err != nil { |
| 276 | + return nil, fmt.Errorf("Error constructing id: %s", err) |
| 277 | + } |
| 278 | + d.SetId(id) |
| 279 | + |
| 280 | + return []*schema.ResourceData{d}, nil |
| 281 | +} |
| 282 | + |
| 283 | +func flattenComputePublicAdvertisedPrefixDescription(v interface{}, d *schema.ResourceData, config *Config) interface{} { |
| 284 | + return v |
| 285 | +} |
| 286 | + |
| 287 | +func flattenComputePublicAdvertisedPrefixName(v interface{}, d *schema.ResourceData, config *Config) interface{} { |
| 288 | + return v |
| 289 | +} |
| 290 | + |
| 291 | +func flattenComputePublicAdvertisedPrefixDnsVerificationIp(v interface{}, d *schema.ResourceData, config *Config) interface{} { |
| 292 | + return v |
| 293 | +} |
| 294 | + |
| 295 | +func flattenComputePublicAdvertisedPrefixIpCidrRange(v interface{}, d *schema.ResourceData, config *Config) interface{} { |
| 296 | + return v |
| 297 | +} |
| 298 | + |
| 299 | +func expandComputePublicAdvertisedPrefixDescription(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { |
| 300 | + return v, nil |
| 301 | +} |
| 302 | + |
| 303 | +func expandComputePublicAdvertisedPrefixName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { |
| 304 | + return v, nil |
| 305 | +} |
| 306 | + |
| 307 | +func expandComputePublicAdvertisedPrefixDnsVerificationIp(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { |
| 308 | + return v, nil |
| 309 | +} |
| 310 | + |
| 311 | +func expandComputePublicAdvertisedPrefixIpCidrRange(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { |
| 312 | + return v, nil |
| 313 | +} |
0 commit comments