github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/google/resource_compute_ssl_certificate.go (about) 1 package google 2 3 import ( 4 "fmt" 5 "log" 6 "strconv" 7 8 "github.com/hashicorp/terraform/helper/schema" 9 "google.golang.org/api/compute/v1" 10 "google.golang.org/api/googleapi" 11 ) 12 13 func resourceComputeSslCertificate() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceComputeSslCertificateCreate, 16 Read: resourceComputeSslCertificateRead, 17 Delete: resourceComputeSslCertificateDelete, 18 19 Schema: map[string]*schema.Schema{ 20 "certificate": &schema.Schema{ 21 Type: schema.TypeString, 22 Required: true, 23 ForceNew: true, 24 }, 25 26 "name": &schema.Schema{ 27 Type: schema.TypeString, 28 Required: true, 29 ForceNew: true, 30 }, 31 32 "private_key": &schema.Schema{ 33 Type: schema.TypeString, 34 Required: true, 35 ForceNew: true, 36 }, 37 38 "description": &schema.Schema{ 39 Type: schema.TypeString, 40 Optional: true, 41 ForceNew: true, 42 }, 43 44 "id": &schema.Schema{ 45 Type: schema.TypeString, 46 Computed: true, 47 }, 48 49 "project": &schema.Schema{ 50 Type: schema.TypeString, 51 Optional: true, 52 ForceNew: true, 53 }, 54 55 "self_link": &schema.Schema{ 56 Type: schema.TypeString, 57 Computed: true, 58 }, 59 }, 60 } 61 } 62 63 func resourceComputeSslCertificateCreate(d *schema.ResourceData, meta interface{}) error { 64 config := meta.(*Config) 65 66 project, err := getProject(d, config) 67 if err != nil { 68 return err 69 } 70 71 // Build the certificate parameter 72 cert := &compute.SslCertificate{ 73 Name: d.Get("name").(string), 74 Certificate: d.Get("certificate").(string), 75 PrivateKey: d.Get("private_key").(string), 76 } 77 78 if v, ok := d.GetOk("description"); ok { 79 cert.Description = v.(string) 80 } 81 82 op, err := config.clientCompute.SslCertificates.Insert( 83 project, cert).Do() 84 85 if err != nil { 86 return fmt.Errorf("Error creating ssl certificate: %s", err) 87 } 88 89 err = computeOperationWaitGlobal(config, op, project, "Creating SslCertificate") 90 if err != nil { 91 return err 92 } 93 94 d.SetId(cert.Name) 95 96 return resourceComputeSslCertificateRead(d, meta) 97 } 98 99 func resourceComputeSslCertificateRead(d *schema.ResourceData, meta interface{}) error { 100 config := meta.(*Config) 101 102 project, err := getProject(d, config) 103 if err != nil { 104 return err 105 } 106 107 cert, err := config.clientCompute.SslCertificates.Get( 108 project, d.Id()).Do() 109 if err != nil { 110 if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { 111 log.Printf("[WARN] Removing SSL Certificate %q because it's gone", d.Get("name").(string)) 112 // The resource doesn't exist anymore 113 d.SetId("") 114 115 return nil 116 } 117 118 return fmt.Errorf("Error reading ssl certificate: %s", err) 119 } 120 121 d.Set("self_link", cert.SelfLink) 122 d.Set("id", strconv.FormatUint(cert.Id, 10)) 123 124 return nil 125 } 126 127 func resourceComputeSslCertificateDelete(d *schema.ResourceData, meta interface{}) error { 128 config := meta.(*Config) 129 130 project, err := getProject(d, config) 131 if err != nil { 132 return err 133 } 134 135 op, err := config.clientCompute.SslCertificates.Delete( 136 project, d.Id()).Do() 137 if err != nil { 138 return fmt.Errorf("Error deleting ssl certificate: %s", err) 139 } 140 141 err = computeOperationWaitGlobal(config, op, project, "Deleting SslCertificate") 142 if err != nil { 143 return err 144 } 145 146 d.SetId("") 147 return nil 148 }