Skip to content

Commit 34943cb

Browse files
committed
Add import support for ssl certificate, http/s proxy and url map
1 parent c889e7d commit 34943cb

9 files changed

+200
-88
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package google
2+
3+
import (
4+
"github.com/hashicorp/terraform/helper/resource"
5+
"testing"
6+
)
7+
8+
func TestAccComputeSslCertificate_import(t *testing.T) {
9+
t.Parallel()
10+
11+
resource.Test(t, resource.TestCase{
12+
PreCheck: func() { testAccPreCheck(t) },
13+
Providers: testAccProviders,
14+
CheckDestroy: testAccCheckComputeSslCertificateDestroy,
15+
Steps: []resource.TestStep{
16+
resource.TestStep{
17+
Config: testAccComputeSslCertificate_import,
18+
},
19+
resource.TestStep{
20+
ResourceName: "google_compute_ssl_certificate.foobar",
21+
ImportState: true,
22+
ImportStateVerify: true,
23+
ImportStateVerifyIgnore: []string{"private_key"},
24+
},
25+
},
26+
})
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package google
2+
3+
import (
4+
"fmt"
5+
"github.com/hashicorp/terraform/helper/acctest"
6+
"github.com/hashicorp/terraform/helper/resource"
7+
"testing"
8+
)
9+
10+
func TestAccComputeTargetHttpProxy_import(t *testing.T) {
11+
t.Parallel()
12+
13+
target := fmt.Sprintf("thttp-test-%s", acctest.RandString(10))
14+
backend := fmt.Sprintf("thttp-test-%s", acctest.RandString(10))
15+
hc := fmt.Sprintf("thttp-test-%s", acctest.RandString(10))
16+
urlmap1 := fmt.Sprintf("thttp-test-%s", acctest.RandString(10))
17+
urlmap2 := fmt.Sprintf("thttp-test-%s", acctest.RandString(10))
18+
19+
resource.Test(t, resource.TestCase{
20+
PreCheck: func() { testAccPreCheck(t) },
21+
Providers: testAccProviders,
22+
CheckDestroy: testAccCheckComputeTargetHttpProxyDestroy,
23+
Steps: []resource.TestStep{
24+
resource.TestStep{
25+
Config: testAccComputeTargetHttpProxy_basic1(target, backend, hc, urlmap1, urlmap2),
26+
},
27+
resource.TestStep{
28+
ResourceName: "google_compute_target_http_proxy.foobar",
29+
ImportState: true,
30+
ImportStateVerify: true,
31+
},
32+
},
33+
})
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package google
2+
3+
import (
4+
"fmt"
5+
"github.com/hashicorp/terraform/helper/acctest"
6+
"github.com/hashicorp/terraform/helper/resource"
7+
"testing"
8+
)
9+
10+
func TestAccComputeTargetHttpsProxy_import(t *testing.T) {
11+
t.Parallel()
12+
13+
id := fmt.Sprintf("thttps-test-%s", acctest.RandString(10))
14+
15+
resource.Test(t, resource.TestCase{
16+
PreCheck: func() { testAccPreCheck(t) },
17+
Providers: testAccProviders,
18+
CheckDestroy: testAccCheckComputeTargetHttpsProxyDestroy,
19+
Steps: []resource.TestStep{
20+
resource.TestStep{
21+
Config: testAccComputeTargetHttpsProxy_basic1(id),
22+
},
23+
resource.TestStep{
24+
ResourceName: "google_compute_target_https_proxy.foobar",
25+
ImportState: true,
26+
ImportStateVerify: true,
27+
},
28+
},
29+
})
30+
}

google/import_compute_url_map_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package google
2+
3+
import (
4+
"fmt"
5+
"github.com/hashicorp/terraform/helper/acctest"
6+
"github.com/hashicorp/terraform/helper/resource"
7+
"testing"
8+
)
9+
10+
func TestAccComputeUrlMap_import(t *testing.T) {
11+
t.Parallel()
12+
13+
bsName := fmt.Sprintf("bs-test-%s", acctest.RandString(10))
14+
hcName := fmt.Sprintf("hc-test-%s", acctest.RandString(10))
15+
umName := fmt.Sprintf("um-test-%s", acctest.RandString(10))
16+
17+
resource.Test(t, resource.TestCase{
18+
PreCheck: func() { testAccPreCheck(t) },
19+
Providers: testAccProviders,
20+
CheckDestroy: testAccCheckComputeUrlMapDestroy,
21+
Steps: []resource.TestStep{
22+
resource.TestStep{
23+
Config: testAccComputeUrlMap_basic1(bsName, hcName, umName),
24+
},
25+
resource.TestStep{
26+
ResourceName: "google_compute_url_map.foobar",
27+
ImportState: true,
28+
ImportStateVerify: true,
29+
},
30+
}})
31+
}

google/resource_compute_ssl_certificate.go

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ func resourceComputeSslCertificate() *schema.Resource {
1515
Read: resourceComputeSslCertificateRead,
1616
Delete: resourceComputeSslCertificateDelete,
1717

18+
Importer: &schema.ResourceImporter{
19+
State: schema.ImportStatePassthrough,
20+
},
21+
1822
Schema: map[string]*schema.Schema{
1923
"certificate": &schema.Schema{
2024
Type: schema.TypeString,
@@ -141,6 +145,9 @@ func resourceComputeSslCertificateRead(d *schema.ResourceData, meta interface{})
141145

142146
d.Set("self_link", cert.SelfLink)
143147
d.Set("certificate_id", strconv.FormatUint(cert.Id, 10))
148+
d.Set("description", cert.Description)
149+
d.Set("name", cert.Name)
150+
d.Set("certificate", cert.Certificate)
144151

145152
return nil
146153
}

google/resource_compute_ssl_certificate_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,12 @@ resource "google_compute_ssl_certificate" "foobar" {
136136
certificate = "${file("test-fixtures/ssl_cert/test.crt")}"
137137
}
138138
`, acctest.RandString(10))
139+
140+
var testAccComputeSslCertificate_import = fmt.Sprintf(`
141+
resource "google_compute_ssl_certificate" "foobar" {
142+
name = "sslcert-test-%s"
143+
description = "very descriptive"
144+
private_key = "${file("test-fixtures/ssl_cert/test.key")}"
145+
certificate = "${file("test-fixtures/ssl_cert/test.crt")}"
146+
}
147+
`, acctest.RandString(10))

google/resource_compute_target_http_proxy.go

+7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ func resourceComputeTargetHttpProxy() *schema.Resource {
1616
Delete: resourceComputeTargetHttpProxyDelete,
1717
Update: resourceComputeTargetHttpProxyUpdate,
1818

19+
Importer: &schema.ResourceImporter{
20+
State: schema.ImportStatePassthrough,
21+
},
22+
1923
Schema: map[string]*schema.Schema{
2024
"name": &schema.Schema{
2125
Type: schema.TypeString,
@@ -135,6 +139,9 @@ func resourceComputeTargetHttpProxyRead(d *schema.ResourceData, meta interface{}
135139

136140
d.Set("self_link", proxy.SelfLink)
137141
d.Set("proxy_id", strconv.FormatUint(proxy.Id, 10))
142+
d.Set("description", proxy.Description)
143+
d.Set("url_map", proxy.UrlMap)
144+
d.Set("name", proxy.Name)
138145

139146
return nil
140147
}

google/resource_compute_target_https_proxy.go

+7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ func resourceComputeTargetHttpsProxy() *schema.Resource {
2323
Delete: resourceComputeTargetHttpsProxyDelete,
2424
Update: resourceComputeTargetHttpsProxyUpdate,
2525

26+
Importer: &schema.ResourceImporter{
27+
State: schema.ImportStatePassthrough,
28+
},
29+
2630
Schema: map[string]*schema.Schema{
2731
"name": &schema.Schema{
2832
Type: schema.TypeString,
@@ -180,6 +184,9 @@ func resourceComputeTargetHttpsProxyRead(d *schema.ResourceData, meta interface{
180184
d.Set("ssl_certificates", proxy.SslCertificates)
181185
d.Set("proxy_id", strconv.FormatUint(proxy.Id, 10))
182186
d.Set("self_link", proxy.SelfLink)
187+
d.Set("description", proxy.Description)
188+
d.Set("url_map", proxy.UrlMap)
189+
d.Set("name", proxy.Name)
183190

184191
return nil
185192
}

google/resource_compute_url_map.go

+48-88
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ func resourceComputeUrlMap() *schema.Resource {
1515
Update: resourceComputeUrlMapUpdate,
1616
Delete: resourceComputeUrlMapDelete,
1717

18+
Importer: &schema.ResourceImporter{
19+
State: resourceComputeUrlMapImportState,
20+
},
21+
1822
Schema: map[string]*schema.Schema{
1923
"default_service": &schema.Schema{
2024
Type: schema.TypeString,
@@ -317,112 +321,63 @@ func resourceComputeUrlMapRead(d *schema.ResourceData, meta interface{}) error {
317321
d.Set("self_link", urlMap.SelfLink)
318322
d.Set("map_id", strconv.FormatUint(urlMap.Id, 10))
319323
d.Set("fingerprint", urlMap.Fingerprint)
324+
d.Set("default_service", urlMap.DefaultService)
320325

321-
hostRuleMap := make(map[string]*compute.HostRule)
322-
for _, v := range urlMap.HostRules {
323-
hostRuleMap[v.PathMatcher] = v
324-
}
325-
326-
/* Only read host rules into our TF state that we have defined */
327-
_hostRules := d.Get("host_rule").(*schema.Set).List()
328326
_newHostRules := make([]interface{}, 0)
329-
for _, v := range _hostRules {
330-
_hostRule := v.(map[string]interface{})
331-
_pathMatcher := _hostRule["path_matcher"].(string)
332-
333-
/* Delete local entries that are no longer found on the GCE server */
334-
if hostRule, ok := hostRuleMap[_pathMatcher]; ok {
335-
_newHostRule := make(map[string]interface{})
336-
_newHostRule["path_matcher"] = _pathMatcher
337-
338-
hostsSet := make(map[string]bool)
339-
for _, host := range hostRule.Hosts {
340-
hostsSet[host] = true
341-
}
327+
for _, hostRule := range urlMap.HostRules {
328+
_newHostRule := make(map[string]interface{})
329+
_newHostRule["path_matcher"] = hostRule.PathMatcher
342330

343-
/* Only store hosts we are keeping track of */
344-
_newHosts := make([]interface{}, 0)
345-
for _, vp := range _hostRule["hosts"].([]interface{}) {
346-
if _, okp := hostsSet[vp.(string)]; okp {
347-
_newHosts = append(_newHosts, vp)
348-
}
349-
}
331+
_newHosts := make([]interface{}, 0)
332+
for _, host := range hostRule.Hosts {
333+
_newHosts = append(_newHosts, host)
334+
}
350335

351-
_newHostRule["hosts"] = _newHosts
352-
_newHostRule["description"] = hostRule.Description
336+
_newHostRule["hosts"] = _newHosts
337+
_newHostRule["description"] = hostRule.Description
353338

354-
_newHostRules = append(_newHostRules, _newHostRule)
355-
}
339+
_newHostRules = append(_newHostRules, _newHostRule)
356340
}
357341

358342
d.Set("host_rule", _newHostRules)
359343

360-
pathMatcherMap := make(map[string]*compute.PathMatcher)
361-
for _, v := range urlMap.PathMatchers {
362-
pathMatcherMap[v.Name] = v
363-
}
364-
365-
/* Only read path matchers into our TF state that we have defined */
366-
_pathMatchers := d.Get("path_matcher").([]interface{})
367344
_newPathMatchers := make([]interface{}, 0)
368-
for _, v := range _pathMatchers {
369-
_pathMatcher := v.(map[string]interface{})
370-
_name := _pathMatcher["name"].(string)
371-
372-
if pathMatcher, ok := pathMatcherMap[_name]; ok {
373-
_newPathMatcher := make(map[string]interface{})
374-
_newPathMatcher["name"] = _name
375-
_newPathMatcher["default_service"] = pathMatcher.DefaultService
376-
_newPathMatcher["description"] = pathMatcher.Description
377-
378-
_newPathRules := make([]interface{}, len(pathMatcher.PathRules))
379-
for ip, pathRule := range pathMatcher.PathRules {
380-
_newPathRule := make(map[string]interface{})
381-
_newPathRule["service"] = pathRule.Service
382-
_paths := make([]interface{}, len(pathRule.Paths))
383-
384-
for ipp, vpp := range pathRule.Paths {
385-
_paths[ipp] = vpp
386-
}
387-
388-
_newPathRule["paths"] = _paths
389-
390-
_newPathRules[ip] = _newPathRule
345+
for _, pathMatcher := range urlMap.PathMatchers {
346+
_newPathMatcher := make(map[string]interface{})
347+
_newPathMatcher["name"] = pathMatcher.Name
348+
_newPathMatcher["default_service"] = pathMatcher.DefaultService
349+
_newPathMatcher["description"] = pathMatcher.Description
350+
351+
_newPathRules := make([]interface{}, len(pathMatcher.PathRules))
352+
for ip, pathRule := range pathMatcher.PathRules {
353+
_newPathRule := make(map[string]interface{})
354+
_newPathRule["service"] = pathRule.Service
355+
_paths := make([]interface{}, len(pathRule.Paths))
356+
357+
for ipp, vpp := range pathRule.Paths {
358+
_paths[ipp] = vpp
391359
}
392360

393-
_newPathMatcher["path_rule"] = _newPathRules
394-
_newPathMatchers = append(_newPathMatchers, _newPathMatcher)
361+
_newPathRule["paths"] = _paths
362+
363+
_newPathRules[ip] = _newPathRule
395364
}
396-
}
397365

398-
d.Set("path_matcher", _newPathMatchers)
366+
_newPathMatcher["path_rule"] = _newPathRules
367+
_newPathMatchers = append(_newPathMatchers, _newPathMatcher)
399368

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

405-
_tests := make([]interface{}, 0)
406-
/* Only read tests into our TF state that we have defined */
407-
if v, ok := d.GetOk("test"); ok {
408-
_tests = v.([]interface{})
409-
}
371+
d.Set("path_matcher", _newPathMatchers)
372+
410373
_newTests := make([]interface{}, 0)
411-
for _, v := range _tests {
412-
_test := v.(map[string]interface{})
413-
_host := _test["host"].(string)
414-
_path := _test["path"].(string)
415-
416-
/* Delete local entries that are no longer found on the GCE server */
417-
if test, ok := testMap[fmt.Sprintf("%s/%s", _host, _path)]; ok {
418-
_newTest := make(map[string]interface{})
419-
_newTest["host"] = _host
420-
_newTest["path"] = _path
421-
_newTest["description"] = test.Description
422-
_newTest["service"] = test.Service
423-
424-
_newTests = append(_newTests, _newTest)
425-
}
374+
for _, test := range urlMap.Tests {
375+
_newTest := make(map[string]interface{})
376+
_newTest["host"] = test.Host
377+
_newTest["path"] = test.Path
378+
_newTest["description"] = test.Description
379+
_newTest["service"] = test.Service
380+
_newTests = append(_newTests, _newTest)
426381
}
427382

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

634+
func resourceComputeUrlMapImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
635+
d.Set("name", d.Id())
636+
return []*schema.ResourceData{d}, nil
637+
}
638+
679639
func validateHostRules(v interface{}, k string) (ws []string, es []error) {
680640
pathMatchers := make(map[string]bool)
681641
hostRules := v.([]interface{})

0 commit comments

Comments
 (0)