Skip to content

Commit

Permalink
Add import support for ssl certificate, http/s proxy and url map
Browse files Browse the repository at this point in the history
  • Loading branch information
krotkiewicz committed Nov 3, 2017
1 parent c889e7d commit 34943cb
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 88 deletions.
27 changes: 27 additions & 0 deletions google/import_compute_ssl_certificate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package google

import (
"github.com/hashicorp/terraform/helper/resource"
"testing"
)

func TestAccComputeSslCertificate_import(t *testing.T) {
t.Parallel()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeSslCertificateDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeSslCertificate_import,
},
resource.TestStep{
ResourceName: "google_compute_ssl_certificate.foobar",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"private_key"},
},
},
})
}
34 changes: 34 additions & 0 deletions google/import_compute_target_http_proxy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package google

import (
"fmt"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"testing"
)

func TestAccComputeTargetHttpProxy_import(t *testing.T) {
t.Parallel()

target := fmt.Sprintf("thttp-test-%s", acctest.RandString(10))
backend := fmt.Sprintf("thttp-test-%s", acctest.RandString(10))
hc := fmt.Sprintf("thttp-test-%s", acctest.RandString(10))
urlmap1 := fmt.Sprintf("thttp-test-%s", acctest.RandString(10))
urlmap2 := fmt.Sprintf("thttp-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeTargetHttpProxyDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeTargetHttpProxy_basic1(target, backend, hc, urlmap1, urlmap2),
},
resource.TestStep{
ResourceName: "google_compute_target_http_proxy.foobar",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
30 changes: 30 additions & 0 deletions google/import_compute_target_https_proxy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package google

import (
"fmt"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"testing"
)

func TestAccComputeTargetHttpsProxy_import(t *testing.T) {
t.Parallel()

id := fmt.Sprintf("thttps-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeTargetHttpsProxyDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeTargetHttpsProxy_basic1(id),
},
resource.TestStep{
ResourceName: "google_compute_target_https_proxy.foobar",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
31 changes: 31 additions & 0 deletions google/import_compute_url_map_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package google

import (
"fmt"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"testing"
)

func TestAccComputeUrlMap_import(t *testing.T) {
t.Parallel()

bsName := fmt.Sprintf("bs-test-%s", acctest.RandString(10))
hcName := fmt.Sprintf("hc-test-%s", acctest.RandString(10))
umName := fmt.Sprintf("um-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeUrlMapDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeUrlMap_basic1(bsName, hcName, umName),
},
resource.TestStep{
ResourceName: "google_compute_url_map.foobar",
ImportState: true,
ImportStateVerify: true,
},
}})
}
7 changes: 7 additions & 0 deletions google/resource_compute_ssl_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ func resourceComputeSslCertificate() *schema.Resource {
Read: resourceComputeSslCertificateRead,
Delete: resourceComputeSslCertificateDelete,

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

Schema: map[string]*schema.Schema{
"certificate": &schema.Schema{
Type: schema.TypeString,
Expand Down Expand Up @@ -141,6 +145,9 @@ func resourceComputeSslCertificateRead(d *schema.ResourceData, meta interface{})

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)

return nil
}
Expand Down
9 changes: 9 additions & 0 deletions google/resource_compute_ssl_certificate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,12 @@ resource "google_compute_ssl_certificate" "foobar" {
certificate = "${file("test-fixtures/ssl_cert/test.crt")}"
}
`, acctest.RandString(10))

var testAccComputeSslCertificate_import = fmt.Sprintf(`
resource "google_compute_ssl_certificate" "foobar" {
name = "sslcert-test-%s"
description = "very descriptive"
private_key = "${file("test-fixtures/ssl_cert/test.key")}"
certificate = "${file("test-fixtures/ssl_cert/test.crt")}"
}
`, acctest.RandString(10))
7 changes: 7 additions & 0 deletions google/resource_compute_target_http_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ func resourceComputeTargetHttpProxy() *schema.Resource {
Delete: resourceComputeTargetHttpProxyDelete,
Update: resourceComputeTargetHttpProxyUpdate,

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

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Expand Down Expand Up @@ -135,6 +139,9 @@ func resourceComputeTargetHttpProxyRead(d *schema.ResourceData, meta interface{}

d.Set("self_link", proxy.SelfLink)
d.Set("proxy_id", strconv.FormatUint(proxy.Id, 10))
d.Set("description", proxy.Description)
d.Set("url_map", proxy.UrlMap)
d.Set("name", proxy.Name)

return nil
}
Expand Down
7 changes: 7 additions & 0 deletions google/resource_compute_target_https_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ func resourceComputeTargetHttpsProxy() *schema.Resource {
Delete: resourceComputeTargetHttpsProxyDelete,
Update: resourceComputeTargetHttpsProxyUpdate,

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

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Expand Down Expand Up @@ -180,6 +184,9 @@ func resourceComputeTargetHttpsProxyRead(d *schema.ResourceData, meta interface{
d.Set("ssl_certificates", proxy.SslCertificates)
d.Set("proxy_id", strconv.FormatUint(proxy.Id, 10))
d.Set("self_link", proxy.SelfLink)
d.Set("description", proxy.Description)
d.Set("url_map", proxy.UrlMap)
d.Set("name", proxy.Name)

return nil
}
Expand Down
136 changes: 48 additions & 88 deletions google/resource_compute_url_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ func resourceComputeUrlMap() *schema.Resource {
Update: resourceComputeUrlMapUpdate,
Delete: resourceComputeUrlMapDelete,

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

Schema: map[string]*schema.Schema{
"default_service": &schema.Schema{
Type: schema.TypeString,
Expand Down Expand Up @@ -317,112 +321,63 @@ func resourceComputeUrlMapRead(d *schema.ResourceData, meta interface{}) error {
d.Set("self_link", urlMap.SelfLink)
d.Set("map_id", strconv.FormatUint(urlMap.Id, 10))
d.Set("fingerprint", urlMap.Fingerprint)
d.Set("default_service", urlMap.DefaultService)

hostRuleMap := make(map[string]*compute.HostRule)
for _, v := range urlMap.HostRules {
hostRuleMap[v.PathMatcher] = v
}

/* Only read host rules into our TF state that we have defined */
_hostRules := d.Get("host_rule").(*schema.Set).List()
_newHostRules := make([]interface{}, 0)
for _, v := range _hostRules {
_hostRule := v.(map[string]interface{})
_pathMatcher := _hostRule["path_matcher"].(string)

/* Delete local entries that are no longer found on the GCE server */
if hostRule, ok := hostRuleMap[_pathMatcher]; ok {
_newHostRule := make(map[string]interface{})
_newHostRule["path_matcher"] = _pathMatcher

hostsSet := make(map[string]bool)
for _, host := range hostRule.Hosts {
hostsSet[host] = true
}
for _, hostRule := range urlMap.HostRules {
_newHostRule := make(map[string]interface{})
_newHostRule["path_matcher"] = hostRule.PathMatcher

/* Only store hosts we are keeping track of */
_newHosts := make([]interface{}, 0)
for _, vp := range _hostRule["hosts"].([]interface{}) {
if _, okp := hostsSet[vp.(string)]; okp {
_newHosts = append(_newHosts, vp)
}
}
_newHosts := make([]interface{}, 0)
for _, host := range hostRule.Hosts {
_newHosts = append(_newHosts, host)
}

_newHostRule["hosts"] = _newHosts
_newHostRule["description"] = hostRule.Description
_newHostRule["hosts"] = _newHosts
_newHostRule["description"] = hostRule.Description

_newHostRules = append(_newHostRules, _newHostRule)
}
_newHostRules = append(_newHostRules, _newHostRule)
}

d.Set("host_rule", _newHostRules)

pathMatcherMap := make(map[string]*compute.PathMatcher)
for _, v := range urlMap.PathMatchers {
pathMatcherMap[v.Name] = v
}

/* Only read path matchers into our TF state that we have defined */
_pathMatchers := d.Get("path_matcher").([]interface{})
_newPathMatchers := make([]interface{}, 0)
for _, v := range _pathMatchers {
_pathMatcher := v.(map[string]interface{})
_name := _pathMatcher["name"].(string)

if pathMatcher, ok := pathMatcherMap[_name]; ok {
_newPathMatcher := make(map[string]interface{})
_newPathMatcher["name"] = _name
_newPathMatcher["default_service"] = pathMatcher.DefaultService
_newPathMatcher["description"] = pathMatcher.Description

_newPathRules := make([]interface{}, len(pathMatcher.PathRules))
for ip, pathRule := range pathMatcher.PathRules {
_newPathRule := make(map[string]interface{})
_newPathRule["service"] = pathRule.Service
_paths := make([]interface{}, len(pathRule.Paths))

for ipp, vpp := range pathRule.Paths {
_paths[ipp] = vpp
}

_newPathRule["paths"] = _paths

_newPathRules[ip] = _newPathRule
for _, pathMatcher := range urlMap.PathMatchers {
_newPathMatcher := make(map[string]interface{})
_newPathMatcher["name"] = pathMatcher.Name
_newPathMatcher["default_service"] = pathMatcher.DefaultService
_newPathMatcher["description"] = pathMatcher.Description

_newPathRules := make([]interface{}, len(pathMatcher.PathRules))
for ip, pathRule := range pathMatcher.PathRules {
_newPathRule := make(map[string]interface{})
_newPathRule["service"] = pathRule.Service
_paths := make([]interface{}, len(pathRule.Paths))

for ipp, vpp := range pathRule.Paths {
_paths[ipp] = vpp
}

_newPathMatcher["path_rule"] = _newPathRules
_newPathMatchers = append(_newPathMatchers, _newPathMatcher)
_newPathRule["paths"] = _paths

_newPathRules[ip] = _newPathRule
}
}

d.Set("path_matcher", _newPathMatchers)
_newPathMatcher["path_rule"] = _newPathRules
_newPathMatchers = append(_newPathMatchers, _newPathMatcher)

testMap := make(map[string]*compute.UrlMapTest)
for _, v := range urlMap.Tests {
testMap[fmt.Sprintf("%s/%s", v.Host, v.Path)] = v
}

_tests := make([]interface{}, 0)
/* Only read tests into our TF state that we have defined */
if v, ok := d.GetOk("test"); ok {
_tests = v.([]interface{})
}
d.Set("path_matcher", _newPathMatchers)

_newTests := make([]interface{}, 0)
for _, v := range _tests {
_test := v.(map[string]interface{})
_host := _test["host"].(string)
_path := _test["path"].(string)

/* Delete local entries that are no longer found on the GCE server */
if test, ok := testMap[fmt.Sprintf("%s/%s", _host, _path)]; ok {
_newTest := make(map[string]interface{})
_newTest["host"] = _host
_newTest["path"] = _path
_newTest["description"] = test.Description
_newTest["service"] = test.Service

_newTests = append(_newTests, _newTest)
}
for _, test := range urlMap.Tests {
_newTest := make(map[string]interface{})
_newTest["host"] = test.Host
_newTest["path"] = test.Path
_newTest["description"] = test.Description
_newTest["service"] = test.Service
_newTests = append(_newTests, _newTest)
}

d.Set("test", _newTests)
Expand Down Expand Up @@ -676,6 +631,11 @@ func resourceComputeUrlMapDelete(d *schema.ResourceData, meta interface{}) error
return nil
}

func resourceComputeUrlMapImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
d.Set("name", d.Id())
return []*schema.ResourceData{d}, nil
}

func validateHostRules(v interface{}, k string) (ws []string, es []error) {
pathMatchers := make(map[string]bool)
hostRules := v.([]interface{})
Expand Down

0 comments on commit 34943cb

Please sign in to comment.