From 34943cb1b45d64ff62f2dfc544e99dabbe4556cb Mon Sep 17 00:00:00 2001 From: Konrad Rotkiewicz Date: Fri, 3 Nov 2017 13:28:29 +0100 Subject: [PATCH] Add import support for ssl certificate, http/s proxy and url map --- google/import_compute_ssl_certificate_test.go | 27 ++++ .../import_compute_target_http_proxy_test.go | 34 +++++ .../import_compute_target_https_proxy_test.go | 30 ++++ google/import_compute_url_map_test.go | 31 ++++ google/resource_compute_ssl_certificate.go | 7 + .../resource_compute_ssl_certificate_test.go | 9 ++ google/resource_compute_target_http_proxy.go | 7 + google/resource_compute_target_https_proxy.go | 7 + google/resource_compute_url_map.go | 136 +++++++----------- 9 files changed, 200 insertions(+), 88 deletions(-) create mode 100644 google/import_compute_ssl_certificate_test.go create mode 100644 google/import_compute_target_http_proxy_test.go create mode 100644 google/import_compute_target_https_proxy_test.go create mode 100644 google/import_compute_url_map_test.go diff --git a/google/import_compute_ssl_certificate_test.go b/google/import_compute_ssl_certificate_test.go new file mode 100644 index 00000000000..5c53642045d --- /dev/null +++ b/google/import_compute_ssl_certificate_test.go @@ -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"}, + }, + }, + }) +} diff --git a/google/import_compute_target_http_proxy_test.go b/google/import_compute_target_http_proxy_test.go new file mode 100644 index 00000000000..a0a415623e4 --- /dev/null +++ b/google/import_compute_target_http_proxy_test.go @@ -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, + }, + }, + }) +} diff --git a/google/import_compute_target_https_proxy_test.go b/google/import_compute_target_https_proxy_test.go new file mode 100644 index 00000000000..be2eea5aa91 --- /dev/null +++ b/google/import_compute_target_https_proxy_test.go @@ -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, + }, + }, + }) +} diff --git a/google/import_compute_url_map_test.go b/google/import_compute_url_map_test.go new file mode 100644 index 00000000000..f4f72f62b84 --- /dev/null +++ b/google/import_compute_url_map_test.go @@ -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, + }, + }}) +} diff --git a/google/resource_compute_ssl_certificate.go b/google/resource_compute_ssl_certificate.go index 7a7dc2e24da..8610c91ee36 100644 --- a/google/resource_compute_ssl_certificate.go +++ b/google/resource_compute_ssl_certificate.go @@ -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, @@ -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 } diff --git a/google/resource_compute_ssl_certificate_test.go b/google/resource_compute_ssl_certificate_test.go index f26fe267360..673f606ff4e 100644 --- a/google/resource_compute_ssl_certificate_test.go +++ b/google/resource_compute_ssl_certificate_test.go @@ -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)) diff --git a/google/resource_compute_target_http_proxy.go b/google/resource_compute_target_http_proxy.go index 21a9a0a8ae9..be31393a1be 100644 --- a/google/resource_compute_target_http_proxy.go +++ b/google/resource_compute_target_http_proxy.go @@ -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, @@ -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 } diff --git a/google/resource_compute_target_https_proxy.go b/google/resource_compute_target_https_proxy.go index 1c86def40a1..6ec8c2bcbba 100644 --- a/google/resource_compute_target_https_proxy.go +++ b/google/resource_compute_target_https_proxy.go @@ -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, @@ -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 } diff --git a/google/resource_compute_url_map.go b/google/resource_compute_url_map.go index 22933c07e84..646f9a7e82c 100644 --- a/google/resource_compute_url_map.go +++ b/google/resource_compute_url_map.go @@ -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, @@ -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) @@ -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{})