Add importability for Load Balancers and Certificate. (#104)
* Added importability for load balancer * Added importability, go fmt'ed * Added importability for DNS records * added importability for certificates * Added import notes for DNS records and load balancers * Added import tests * Added random int for test configs * Added certificates for the test to import * Fixed docs, added certificate importability on docs * Make TestAccDigitalOceanCertificate_importBasic pass by reusing certificate generation from resource_digitalocean_certificate_test.go * Fix importablity of Load Balancer Droplet droplet_ids. * Revert "Added importability for DNS records" This reverts commit 94b1f69fe756c862125d0b899d49eca74f7bd591. * Remove mention of record imports in docs. * Remove digitalocean/import_digitalocean_record_test.go
This commit is contained in:
parent
4d11c0df85
commit
1a5c46a1ac
|
@ -0,0 +1,35 @@
|
|||
package digitalocean
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/acctest"
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
)
|
||||
|
||||
func TestAccDigitalOceanCertificate_importBasic(t *testing.T) {
|
||||
resourceName := "digitalocean_certificate.foobar"
|
||||
rInt := acctest.RandInt()
|
||||
|
||||
privateKeyMaterial, leafCertMaterial, certChainMaterial := generateTestCertMaterial(t)
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckDigitalOceanCertificateDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccCheckDigitalOceanCertificateConfig_basic(rInt, privateKeyMaterial, leafCertMaterial, certChainMaterial),
|
||||
},
|
||||
|
||||
{
|
||||
ResourceName: resourceName,
|
||||
ImportState: true,
|
||||
ImportStateVerify: true,
|
||||
ImportStateVerifyIgnore: []string{
|
||||
"certificate_chain", "leaf_certificate", "private_key"}, // We ignore these as they are not returned by the API
|
||||
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package digitalocean
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/acctest"
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
)
|
||||
|
||||
func TestAccDigitalOceanLoadBalancer_importBasic(t *testing.T) {
|
||||
resourceName := "digitalocean_loadbalancer.foobar"
|
||||
rInt := acctest.RandInt()
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckDigitalOceanLoadbalancerDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccCheckDigitalOceanLoadbalancerConfig_basic(rInt),
|
||||
},
|
||||
|
||||
{
|
||||
ResourceName: resourceName,
|
||||
ImportState: true,
|
||||
ImportStateVerify: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
|
@ -3,6 +3,7 @@ package digitalocean
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/digitalocean/godo"
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
|
@ -82,11 +83,12 @@ func expandForwardingRules(config []interface{}) []godo.ForwardingRule {
|
|||
}
|
||||
|
||||
func flattenDropletIds(list []int) []interface{} {
|
||||
vs := make([]interface{}, 0, len(list))
|
||||
flatList := make([]interface{}, 0, len(list))
|
||||
for _, v := range list {
|
||||
vs = append(vs, v)
|
||||
vStr := strconv.Itoa(v)
|
||||
flatList = append(flatList, vStr)
|
||||
}
|
||||
return vs
|
||||
return flatList
|
||||
}
|
||||
|
||||
func flattenHealthChecks(health *godo.HealthCheck) []map[string]interface{} {
|
||||
|
|
|
@ -14,6 +14,9 @@ func resourceDigitalOceanCertificate() *schema.Resource {
|
|||
Create: resourceDigitalOceanCertificateCreate,
|
||||
Read: resourceDigitalOceanCertificateRead,
|
||||
Delete: resourceDigitalOceanCertificateDelete,
|
||||
Importer: &schema.ResourceImporter{
|
||||
State: schema.ImportStatePassthrough,
|
||||
},
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
"name": {
|
||||
|
|
|
@ -15,15 +15,7 @@ import (
|
|||
func TestAccDigitalOceanCertificate_Basic(t *testing.T) {
|
||||
var cert godo.Certificate
|
||||
rInt := acctest.RandInt()
|
||||
leafCertMaterial, privateKeyMaterial, err := acctest.RandTLSCert("Acme Co")
|
||||
if err != nil {
|
||||
t.Fatalf("Cannot generate test TLS certificate: %s", err)
|
||||
}
|
||||
rootCertMaterial, _, err := acctest.RandTLSCert("Acme Go")
|
||||
if err != nil {
|
||||
t.Fatalf("Cannot generate test TLS certificate: %s", err)
|
||||
}
|
||||
certChainMaterial := fmt.Sprintf("%s\n%s", strings.TrimSpace(rootCertMaterial), leafCertMaterial)
|
||||
privateKeyMaterial, leafCertMaterial, certChainMaterial := generateTestCertMaterial(t)
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
|
@ -97,6 +89,20 @@ func testAccCheckDigitalOceanCertificateExists(n string, cert *godo.Certificate)
|
|||
}
|
||||
}
|
||||
|
||||
func generateTestCertMaterial(t *testing.T) (string, string, string) {
|
||||
leafCertMaterial, privateKeyMaterial, err := acctest.RandTLSCert("Acme Co")
|
||||
if err != nil {
|
||||
t.Fatalf("Cannot generate test TLS certificate: %s", err)
|
||||
}
|
||||
rootCertMaterial, _, err := acctest.RandTLSCert("Acme Go")
|
||||
if err != nil {
|
||||
t.Fatalf("Cannot generate test TLS certificate: %s", err)
|
||||
}
|
||||
certChainMaterial := fmt.Sprintf("%s\n%s", strings.TrimSpace(rootCertMaterial), leafCertMaterial)
|
||||
|
||||
return privateKeyMaterial, leafCertMaterial, certChainMaterial
|
||||
}
|
||||
|
||||
func testAccCheckDigitalOceanCertificateConfig_basic(rInt int, privateKeyMaterial, leafCert, certChain string) string {
|
||||
return fmt.Sprintf(`
|
||||
resource "digitalocean_certificate" "foobar" {
|
||||
|
|
|
@ -18,6 +18,9 @@ func resourceDigitalOceanLoadbalancer() *schema.Resource {
|
|||
Read: resourceDigitalOceanLoadbalancerRead,
|
||||
Update: resourceDigitalOceanLoadbalancerUpdate,
|
||||
Delete: resourceDigitalOceanLoadbalancerDelete,
|
||||
Importer: &schema.ResourceImporter{
|
||||
State: schema.ImportStatePassthrough,
|
||||
},
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
"name": {
|
||||
|
@ -252,9 +255,12 @@ func resourceDigitalOceanLoadbalancerRead(d *schema.ResourceData, meta interface
|
|||
d.Set("algorithm", loadbalancer.Algorithm)
|
||||
d.Set("region", loadbalancer.Region.Slug)
|
||||
d.Set("redirect_http_to_https", loadbalancer.RedirectHttpToHttps)
|
||||
d.Set("droplet_ids", flattenDropletIds(loadbalancer.DropletIDs))
|
||||
d.Set("droplet_tag", loadbalancer.Tag)
|
||||
|
||||
if err := d.Set("droplet_ids", flattenDropletIds(loadbalancer.DropletIDs)); err != nil {
|
||||
return fmt.Errorf("[DEBUG] Error setting Load Balancer droplet_ids - error: %#v", err)
|
||||
}
|
||||
|
||||
if err := d.Set("sticky_sessions", flattenStickySessions(loadbalancer.StickySessions)); err != nil {
|
||||
return fmt.Errorf("[DEBUG] Error setting Load Balancer sticky_sessions - error: %#v", err)
|
||||
}
|
||||
|
|
|
@ -63,3 +63,13 @@ The following attributes are exported:
|
|||
* `name` - The name of the certificate
|
||||
* `not_after` - The expiration date of the certificate
|
||||
* `sha1_fingerprint` - The SHA-1 fingerprint of the certificate
|
||||
|
||||
|
||||
|
||||
## Import
|
||||
|
||||
Certificates can be imported using the certificate `id`, e.g.
|
||||
|
||||
```
|
||||
terraform import digitalocean_certificate.mycertificate 892071a0-bb95-49bc-8021-3afd67a210bf
|
||||
```
|
||||
|
|
|
@ -96,3 +96,11 @@ The following attributes are exported:
|
|||
|
||||
* `id` - The ID of the Load Balancer
|
||||
* `ip`- The ip of the Load Balancer
|
||||
|
||||
## Import
|
||||
|
||||
Load Balancers can be imported using the `id`, e.g.
|
||||
|
||||
```
|
||||
terraform import digitalocean_loadbalancer.myloadbalancer 4de7ac8b-495b-4884-9a69-1050c6793cd6
|
||||
```
|
||||
|
|
Loading…
Reference in New Issue