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

Autogenerate google_compute_ssl_certificate. #2015

Merged
merged 1 commit into from
Sep 12, 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
1 change: 1 addition & 0 deletions google/provider_compute_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var GeneratedComputeResourcesMap = map[string]*schema.Resource{
"google_compute_region_disk": resourceComputeRegionDisk(),
"google_compute_route": resourceComputeRoute(),
"google_compute_router": resourceComputeRouter(),
"google_compute_ssl_certificate": resourceComputeSslCertificate(),
"google_compute_ssl_policy": resourceComputeSslPolicy(),
"google_compute_subnetwork": resourceComputeSubnetwork(),
"google_compute_target_http_proxy": resourceComputeTargetHttpProxy(),
Expand Down
288 changes: 219 additions & 69 deletions google/resource_compute_ssl_certificate.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
// ----------------------------------------------------------------------------
//
// *** 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"
"strconv"
"time"

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

func resourceComputeSslCertificate() *schema.Resource {
Expand All @@ -16,27 +33,49 @@ func resourceComputeSslCertificate() *schema.Resource {
Delete: resourceComputeSslCertificateDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
State: resourceComputeSslCertificateImport,
},

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

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

"name": &schema.Schema{
"private_key": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Sensitive: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"name_prefix"},
ValidateFunc: validateGCPName,
ConflictsWith: []string{"name_prefix"},
},

"name_prefix": &schema.Schema{
"certificate_id": {
Type: schema.TypeInt,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formerly a string - is that a problem?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope - since this is Computed it gets magicked into a string anyways and won't change anything.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do make sure to note it in the changelog, though, of course.

Computed: true,
},
"creation_timestamp": {
Type: schema.TypeString,
Computed: true,
},
"name_prefix": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Expand All @@ -53,33 +92,13 @@ func resourceComputeSslCertificate() *schema.Resource {
return
},
},

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

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

"certificate_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},

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

"self_link": &schema.Schema{
"self_link": {
Type: schema.TypeString,
Computed: true,
},
Expand All @@ -90,91 +109,222 @@ func resourceComputeSslCertificate() *schema.Resource {
func resourceComputeSslCertificateCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

project, err := getProject(d, config)
obj := make(map[string]interface{})
certificateProp, err := expandComputeSslCertificateCertificate(d.Get("certificate"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("certificate"); !isEmptyValue(reflect.ValueOf(certificateProp)) && (ok || !reflect.DeepEqual(v, certificateProp)) {
obj["certificate"] = certificateProp
}

var certName string
if v, ok := d.GetOk("name"); ok {
certName = v.(string)
} else if v, ok := d.GetOk("name_prefix"); ok {
certName = resource.PrefixedUniqueId(v.(string))
} else {
certName = resource.UniqueId()
descriptionProp, err := expandComputeSslCertificateDescription(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 certificate parameter
cert := &compute.SslCertificate{
Name: certName,
Certificate: d.Get("certificate").(string),
PrivateKey: d.Get("private_key").(string),
nameProp, err := expandComputeSslCertificateName(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
}
privateKeyProp, err := expandComputeSslCertificatePrivateKey(d.Get("private_key"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("private_key"); !isEmptyValue(reflect.ValueOf(privateKeyProp)) && (ok || !reflect.DeepEqual(v, privateKeyProp)) {
obj["privateKey"] = privateKeyProp
}

if v, ok := d.GetOk("description"); ok {
cert.Description = v.(string)
url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/global/sslCertificates")
if err != nil {
return err
}

op, err := config.clientCompute.SslCertificates.Insert(
project, cert).Do()
log.Printf("[DEBUG] Creating new SslCertificate: %#v", obj)
res, err := sendRequest(config, "POST", url, obj)
if err != nil {
return fmt.Errorf("Error creating SslCertificate: %s", err)
}

// Store the ID now
id, err := replaceVars(d, config, "{{name}}")
if err != nil {
return fmt.Errorf("Error creating ssl certificate: %s", err)
return fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)

err = computeOperationWait(config.clientCompute, op, project, "Creating SslCertificate")
project, err := getProject(d, config)
if err != nil {
return err
}
op := &compute.Operation{}
err = Convert(res, op)
if err != nil {
return err
}

d.SetId(cert.Name)
waitErr := computeOperationWaitTime(
config.clientCompute, op, project, "Creating SslCertificate",
int(d.Timeout(schema.TimeoutCreate).Minutes()))

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

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

return resourceComputeSslCertificateRead(d, meta)
}

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

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

cert, err := config.clientCompute.SslCertificates.Get(
project, d.Id()).Do()
res, err := sendRequest(config, "GET", url, nil)
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("SSL Certificate %q", d.Get("name").(string)))
return handleNotFoundError(err, d, fmt.Sprintf("ComputeSslCertificate %q", d.Id()))
}

d.Set("self_link", cert.SelfLink)
d.Set("certificate_id", strconv.FormatUint(cert.Id, 10))
d.Set("description", cert.Description)
d.Set("name", cert.Name)
d.Set("certificate", cert.Certificate)
d.Set("project", project)
if err := d.Set("certificate", flattenComputeSslCertificateCertificate(res["certificate"])); err != nil {
return fmt.Errorf("Error reading SslCertificate: %s", err)
}
if err := d.Set("creation_timestamp", flattenComputeSslCertificateCreationTimestamp(res["creationTimestamp"])); err != nil {
return fmt.Errorf("Error reading SslCertificate: %s", err)
}
if err := d.Set("description", flattenComputeSslCertificateDescription(res["description"])); err != nil {
return fmt.Errorf("Error reading SslCertificate: %s", err)
}
if err := d.Set("certificate_id", flattenComputeSslCertificateCertificate_id(res["id"])); err != nil {
return fmt.Errorf("Error reading SslCertificate: %s", err)
}
if err := d.Set("name", flattenComputeSslCertificateName(res["name"])); err != nil {
return fmt.Errorf("Error reading SslCertificate: %s", err)
}
if err := d.Set("self_link", ConvertSelfLinkToV1(res["selfLink"].(string))); err != nil {
return fmt.Errorf("Error reading SslCertificate: %s", err)
}
project, err := getProject(d, config)
if err != nil {
return err
}
if err := d.Set("project", project); err != nil {
return fmt.Errorf("Error reading SslCertificate: %s", err)
}

return nil
}

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

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

op, err := config.clientCompute.SslCertificates.Delete(
project, d.Id()).Do()
var obj map[string]interface{}
log.Printf("[DEBUG] Deleting SslCertificate %q", d.Id())
res, err := sendRequest(config, "DELETE", url, obj)
if err != nil {
return handleNotFoundError(err, d, "SslCertificate")
}

project, err := getProject(d, config)
if err != nil {
return err
}
op := &compute.Operation{}
err = Convert(res, op)
if err != nil {
return fmt.Errorf("Error deleting ssl certificate: %s", err)
return err
}

err = computeOperationWait(config.clientCompute, op, project, "Deleting SslCertificate")
err = computeOperationWaitTime(
config.clientCompute, op, project, "Deleting SslCertificate",
int(d.Timeout(schema.TimeoutDelete).Minutes()))

if err != nil {
return err
}

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

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

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

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

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

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

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

func flattenComputeSslCertificateCertificate_id(v interface{}) interface{} {
// Handles the string fixed64 format
if strVal, ok := v.(string); ok {
if intVal, err := strconv.ParseInt(strVal, 10, 64); err == nil {
return intVal
} // let terraform core handle it if we can't convert the string to an int.
}
return v
}

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

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

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

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

func expandComputeSslCertificateName(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
var certName string
if v, ok := d.GetOk("name"); ok {
certName = v.(string)
} else if v, ok := d.GetOk("name_prefix"); ok {
certName = resource.PrefixedUniqueId(v.(string))
} else {
certName = resource.UniqueId()
}

// We need to get the {{name}} into schema to set the ID using ReplaceVars
d.Set("name", certName)

return certName, nil
}

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