Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start autogenerating resource_dns_managed_zone #107

Merged
merged 1 commit into from
Nov 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion google-beta/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func Provider() terraform.ResourceProvider {
GeneratedAccessContextManagerResourcesMap,
// end beta-only products
GeneratedComputeResourcesMap,
GeneratedDnsResourcesMap,
GeneratedRedisResourcesMap,
GeneratedResourceManagerResourcesMap,
GeneratedStorageResourcesMap,
Expand Down Expand Up @@ -168,7 +169,6 @@ func Provider() terraform.ResourceProvider {
"google_dataflow_job": resourceDataflowJob(),
"google_dataproc_cluster": resourceDataprocCluster(),
"google_dataproc_job": resourceDataprocJob(),
"google_dns_managed_zone": resourceDnsManagedZone(),
"google_dns_record_set": resourceDnsRecordSet(),
"google_endpoints_service": resourceEndpointsService(),
"google_folder": resourceGoogleFolder(),
Expand Down
21 changes: 21 additions & 0 deletions google-beta/provider_dns_gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** AUTO GENERATED CODE ***
//
// ----------------------------------------------------------------------------
//
// 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 "github.com/hashicorp/terraform/helper/schema"

var GeneratedDnsResourcesMap = map[string]*schema.Resource{
"google_dns_managed_zone": resourceDnsManagedZone(),
}
235 changes: 165 additions & 70 deletions google-beta/resource_dns_managed_zone.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** AUTO GENERATED CODE ***
//
// ----------------------------------------------------------------------------
//
// 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"

"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/dns/v1"
)

func resourceDnsManagedZone() *schema.Resource {
Expand All @@ -14,162 +28,200 @@ func resourceDnsManagedZone() *schema.Resource {
Read: resourceDnsManagedZoneRead,
Update: resourceDnsManagedZoneUpdate,
Delete: resourceDnsManagedZoneDelete,

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

Schema: map[string]*schema.Schema{
"dns_name": &schema.Schema{
"dns_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"name": &schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"description": &schema.Schema{
"description": {
Type: schema.TypeString,
Optional: true,
Default: "Managed by Terraform",
},

"name_servers": &schema.Schema{
"labels": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"name_servers": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},

// Google Cloud DNS ManagedZone resources do not have a SelfLink attribute.

"project": &schema.Schema{
"project": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},

"labels": &schema.Schema{
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}

func resourceDnsManagedZoneCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

project, err := getProject(d, config)
obj := make(map[string]interface{})
descriptionProp, err := expandDnsManagedZoneDescription(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
}

// Build the parameter
zone := &dns.ManagedZone{
Name: d.Get("name").(string),
DnsName: d.Get("dns_name").(string),
Description: d.Get("description").(string),
dnsNameProp, err := expandDnsManagedZoneDnsName(d.Get("dns_name"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("dns_name"); !isEmptyValue(reflect.ValueOf(dnsNameProp)) && (ok || !reflect.DeepEqual(v, dnsNameProp)) {
obj["dnsName"] = dnsNameProp
}
nameProp, err := expandDnsManagedZoneName(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
}
labelsProp, err := expandDnsManagedZoneLabels(d.Get("labels"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("labels"); !isEmptyValue(reflect.ValueOf(labelsProp)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
}

if _, ok := d.GetOk("labels"); ok {
zone.Labels = expandLabels(d)
url, err := replaceVars(d, config, "https://www.googleapis.com/dns/v1/projects/{{project}}/managedZones")
if err != nil {
return err
}

log.Printf("[DEBUG] DNS ManagedZone create request: %#v", zone)
log.Printf("[DEBUG] Creating new ManagedZone: %#v", obj)
res, err := sendRequest(config, "POST", url, obj)
if err != nil {
return fmt.Errorf("Error creating ManagedZone: %s", err)
}

zone, err = config.clientDns.ManagedZones.Create(project, zone).Do()
// Store the ID now
id, err := replaceVars(d, config, "{{name}}")
if err != nil {
return fmt.Errorf("Error creating DNS ManagedZone: %s", err)
return fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)

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

return resourceDnsManagedZoneRead(d, meta)
}

func resourceDnsManagedZoneRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

project, err := getProject(d, config)
url, err := replaceVars(d, config, "https://www.googleapis.com/dns/v1/projects/{{project}}/managedZones/{{name}}")
if err != nil {
return err
}

zone, err := config.clientDns.ManagedZones.Get(
project, d.Id()).Do()
res, err := sendRequest(config, "GET", url, nil)
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("DNS Managed Zone %q", d.Get("name").(string)))
return handleNotFoundError(err, d, fmt.Sprintf("DnsManagedZone %q", d.Id()))
}

d.Set("name_servers", zone.NameServers)
d.Set("name", zone.Name)
d.Set("dns_name", zone.DnsName)
d.Set("description", zone.Description)
d.Set("project", project)
d.Set("labels", zone.Labels)

return nil
}

func resourceDnsManagedZoneUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

if err := d.Set("description", flattenDnsManagedZoneDescription(res["description"])); err != nil {
return fmt.Errorf("Error reading ManagedZone: %s", err)
}
if err := d.Set("dns_name", flattenDnsManagedZoneDnsName(res["dnsName"])); err != nil {
return fmt.Errorf("Error reading ManagedZone: %s", err)
}
if err := d.Set("name", flattenDnsManagedZoneName(res["name"])); err != nil {
return fmt.Errorf("Error reading ManagedZone: %s", err)
}
if err := d.Set("name_servers", flattenDnsManagedZoneNameServers(res["nameServers"])); err != nil {
return fmt.Errorf("Error reading ManagedZone: %s", err)
}
if err := d.Set("labels", flattenDnsManagedZoneLabels(res["labels"])); err != nil {
return fmt.Errorf("Error reading ManagedZone: %s", err)
}
project, err := getProject(d, config)
if err != nil {
return err
}

zone := &dns.ManagedZone{
Name: d.Get("name").(string),
DnsName: d.Get("dns_name").(string),
Description: d.Get("description").(string),
if err := d.Set("project", project); err != nil {
return fmt.Errorf("Error reading ManagedZone: %s", err)
}

if _, ok := d.GetOk("labels"); ok {
zone.Labels = expandLabels(d)
}
return nil
}

op, err := config.clientDns.ManagedZones.Patch(project, d.Id(), zone).Do()
if err != nil {
return err
}
func resourceDnsManagedZoneUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

err = dnsOperationWait(config.clientDns, op, project, "Updating DNS Managed Zone")
if err != nil {
return err
d.Partial(true)

if d.HasChange("description") || d.HasChange("labels") {
obj := make(map[string]interface{})
descriptionProp, err := expandDnsManagedZoneDescription(d.Get("description"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("description"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, descriptionProp)) {
obj["description"] = descriptionProp
}
labelsProp, err := expandDnsManagedZoneLabels(d.Get("labels"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("labels"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
}

url, err := replaceVars(d, config, "https://www.googleapis.com/dns/v1/projects/{{project}}/managedZones/{{name}}")
if err != nil {
return err
}
_, err = sendRequest(config, "PATCH", url, obj)
if err != nil {
return fmt.Errorf("Error updating ManagedZone %q: %s", d.Id(), err)
}

d.SetPartial("description")
d.SetPartial("labels")
}

d.Partial(false)

return resourceDnsManagedZoneRead(d, meta)
}

func resourceDnsManagedZoneDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

project, err := getProject(d, config)
url, err := replaceVars(d, config, "https://www.googleapis.com/dns/v1/projects/{{project}}/managedZones/{{name}}")
if err != nil {
return err
}

err = config.clientDns.ManagedZones.Delete(project, d.Id()).Do()
var obj map[string]interface{}
log.Printf("[DEBUG] Deleting ManagedZone %q", d.Id())
res, err := sendRequest(config, "DELETE", url, obj)
if err != nil {
return fmt.Errorf("Error deleting DNS ManagedZone: %s", err)
return handleNotFoundError(err, d, "ManagedZone")
}

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

func resourceDnsManagedZoneImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*Config)
parseImportId([]string{"projects/(?P<project>[^/]+)/managedZones/(?P<name>[^/]+)",
"(?P<project>[^/]+)/managedZones/(?P<name>[^/]+)",
"(?P<name>[^/]+)"}, d, config)
parseImportId([]string{"projects/(?P<project>[^/]+)/managedZones/(?P<name>[^/]+)", "(?P<project>[^/]+)/(?P<name>[^/]+)", "(?P<name>[^/]+)"}, d, config)

// Replace import id for the resource id
id, err := replaceVars(d, config, "{{name}}")
Expand All @@ -180,3 +232,46 @@ func resourceDnsManagedZoneImport(d *schema.ResourceData, meta interface{}) ([]*

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

func flattenDnsManagedZoneDescription(v interface{}) interface{} {
return v
}

func flattenDnsManagedZoneDnsName(v interface{}) interface{} {
return v
}

func flattenDnsManagedZoneName(v interface{}) interface{} {
return v
}

func flattenDnsManagedZoneNameServers(v interface{}) interface{} {
return v
}

func flattenDnsManagedZoneLabels(v interface{}) interface{} {
return v
}

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

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

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

func expandDnsManagedZoneLabels(v interface{}, d *schema.ResourceData, config *Config) (map[string]string, error) {
if v == nil {
return map[string]string{}, nil
}
m := make(map[string]string)
for k, val := range v.(map[string]interface{}) {
m[k] = val.(string)
}
return m, nil
}
Loading