Skip to content

Commit b2b535c

Browse files
committed
Sync bitbucket and GitHub
1 parent 44143d1 commit b2b535c

File tree

4 files changed

+510
-34
lines changed

4 files changed

+510
-34
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 23.4.0
2+
* resource/storage_pool: Support terraform import with "poolID:region". ([#88](https://github.com/NetApp/terraform-provider-netapp-gcp/issues/88))
3+
14
## 23.1.0
25
* resource/storage_pool: update and add acceptance test. ([#85](https://github.com/NetApp/terraform-provider-netapp-gcp/issues/85))
36
* resource/volume: Fix for API retry timeout. ([#86](https://github.com/NetApp/terraform-provider-netapp-gcp/issues/86))

gcp/storage_pool.go

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"log"
7+
"strings"
78

89
"github.com/fatih/structs"
910
)
@@ -69,11 +70,10 @@ func (c *Client) createStoragePool(request *storagePool) (storagePool, error) {
6970
return result, nil
7071
}
7172

72-
func (c *Client) getStoragePools(request *storagePool) ([]storagePool, error) {
73-
params := structs.Map(request)
74-
baseURL := fmt.Sprintf("%s/Pools", request.Region)
73+
func (c *Client) getStoragePools(location string) ([]storagePool, error) {
74+
baseURL := fmt.Sprintf("%s/Pools", location)
7575
var result []storagePool
76-
statusCode, response, err := c.CallAPIMethod("GET", baseURL, params)
76+
statusCode, response, err := c.CallAPIMethod("GET", baseURL, nil)
7777
if err != nil {
7878
log.Printf("getStoragePools request failed: %#v", err)
7979
return result, err
@@ -89,7 +89,59 @@ func (c *Client) getStoragePools(request *storagePool) ([]storagePool, error) {
8989
return result, nil
9090
}
9191

92+
// Filter all pools of the project by applying a filter function
93+
func (c *Client) filterAllPools(f func(storagePool) bool) ([]storagePool, error) {
94+
filteredPools := make([]storagePool, 0)
95+
96+
vols, err := c.getStoragePools("-")
97+
if err != nil {
98+
return filteredPools, err
99+
}
100+
101+
for _, v := range vols {
102+
if f(v) {
103+
filteredPools = append(filteredPools, v)
104+
}
105+
}
106+
return filteredPools, nil
107+
}
108+
92109
func (c *Client) getStoragePoolByID(request *storagePool) (storagePool, error) {
110+
var originalID string = ""
111+
// terraform import will specify poolID.
112+
// ID = <poolID>:<region>
113+
s := strings.Split(request.PoolID, ":")
114+
if len(s) == 2 {
115+
originalID = request.PoolID
116+
request.PoolID = s[0]
117+
request.Region = s[1]
118+
}
119+
120+
if request.Region == "" {
121+
// terraform import: ID = <poolID> and no region specified
122+
// find all pools which match poolID
123+
pools, err := c.filterAllPools(func(v storagePool) bool {
124+
return v.PoolID == request.PoolID
125+
})
126+
if err != nil {
127+
return storagePool{}, err
128+
}
129+
130+
if len(pools) == 0 {
131+
return storagePool{}, fmt.Errorf("getStoragePoolByID: No storage pool found with ID %s", request.PoolID)
132+
}
133+
if len(pools) > 1 {
134+
// return error message which tells user to rerun with ID = <poolID>:<region> format
135+
return storagePool{}, fmt.Errorf(`getStoragePoolByID: More than one storage found with ID %s. \n
136+
If this happend while running terraform import, please use \n
137+
terraform import ADDR ID, with ID using <poolID>:<region_name> format`, request.PoolID)
138+
}
139+
if len(pools) == 1 {
140+
// we found the right storage pool to import
141+
request.Region = pools[0].Region
142+
}
143+
}
144+
93145
params := structs.Map(request)
94146
baseURL := fmt.Sprintf("%s/Pools/%s", request.Region, request.PoolID)
95147
var result storagePool
@@ -106,9 +158,24 @@ func (c *Client) getStoragePoolByID(request *storagePool) (storagePool, error) {
106158
log.Printf("Failed to unmarshall response from getStoragePoolByID: %#v", err)
107159
return result, err
108160
}
161+
162+
// poolID is verified by Terraform. If we use ID = <poolID>:<region>, we need to revert our ID changes
163+
if originalID != "" {
164+
result.PoolID = originalID
165+
}
109166
return result, nil
110167
}
111168

169+
func volumeParseID(id string) (string, string, error) {
170+
parts := strings.SplitN(id, ":", 2)
171+
172+
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
173+
return "", "", fmt.Errorf("unexpected format of ID (%s), expected attribute1:attribute2", id)
174+
}
175+
176+
return parts[0], parts[1], nil
177+
}
178+
112179
func (c *Client) deleteStoragePool(request *storagePool) error {
113180
params := structs.Map(request)
114181
baseURL := fmt.Sprintf("%s/Pools/%s", request.Region, request.PoolID)

go.mod

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,49 @@ module github.com/netapp/terraform-provider-netapp-gcp
33
go 1.17
44

55
require (
6-
cloud.google.com/go v0.83.0
6+
cloud.google.com/go v0.105.0
77
github.com/fatih/structs v1.1.0
8-
github.com/gruntwork-io/terratest v0.41.9
8+
github.com/gruntwork-io/terratest v0.41.18
99
github.com/hashicorp/terraform v0.12.28
1010
github.com/sirupsen/logrus v1.8.1
11-
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c
12-
google.golang.org/api v0.47.0
13-
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c
11+
golang.org/x/oauth2 v0.1.0
12+
google.golang.org/api v0.103.0
13+
google.golang.org/genproto v0.0.0-20221201164419-0e50fba7f41c
1414
)
1515

1616
require (
17-
cloud.google.com/go/storage v1.10.0 // indirect
17+
cloud.google.com/go/compute v1.12.1 // indirect
18+
cloud.google.com/go/compute/metadata v0.2.1 // indirect
19+
cloud.google.com/go/iam v0.7.0 // indirect
20+
cloud.google.com/go/storage v1.27.0 // indirect
1821
github.com/agext/levenshtein v1.2.3 // indirect
1922
github.com/apparentlymart/go-cidr v1.0.1 // indirect
2023
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
2124
github.com/armon/go-radix v1.0.0 // indirect
22-
github.com/aws/aws-sdk-go v1.40.56 // indirect
25+
github.com/aws/aws-sdk-go v1.44.122 // indirect
2326
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
2427
github.com/bgentry/speakeasy v0.1.0 // indirect
2528
github.com/blang/semver v3.5.1+incompatible // indirect
2629
github.com/bmatcuk/doublestar v1.1.5 // indirect
2730
github.com/davecgh/go-spew v1.1.1 // indirect
2831
github.com/fatih/color v1.7.0 // indirect
29-
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
32+
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
3033
github.com/golang/protobuf v1.5.2 // indirect
3134
github.com/golang/snappy v0.0.3 // indirect
32-
github.com/google/go-cmp v0.5.7 // indirect
33-
github.com/google/uuid v1.2.0 // indirect
34-
github.com/googleapis/gax-go/v2 v2.0.5 // indirect
35+
github.com/google/go-cmp v0.5.9 // indirect
36+
github.com/google/uuid v1.3.0 // indirect
37+
github.com/googleapis/enterprise-certificate-proxy v0.2.0 // indirect
38+
github.com/googleapis/gax-go/v2 v2.7.0 // indirect
3539
github.com/hashicorp/errwrap v1.0.0 // indirect
3640
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
37-
github.com/hashicorp/go-getter v1.6.1 // indirect
41+
github.com/hashicorp/go-getter v1.7.1 // indirect
3842
github.com/hashicorp/go-hclog v0.0.0-20181001195459-61d530d6c27f // indirect
3943
github.com/hashicorp/go-multierror v1.1.0 // indirect
4044
github.com/hashicorp/go-plugin v1.3.0 // indirect
4145
github.com/hashicorp/go-retryablehttp v0.5.2 // indirect
4246
github.com/hashicorp/go-safetemp v1.0.0 // indirect
4347
github.com/hashicorp/go-uuid v1.0.1 // indirect
44-
github.com/hashicorp/go-version v1.3.0 // indirect
48+
github.com/hashicorp/go-version v1.6.0 // indirect
4549
github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f // indirect
4650
github.com/hashicorp/hcl/v2 v2.9.1 // indirect
4751
github.com/hashicorp/hil v0.0.0-20190212112733-ab17b08d6590 // indirect
@@ -52,15 +56,15 @@ require (
5256
github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a // indirect
5357
github.com/jmespath/go-jmespath v0.4.0 // indirect
5458
github.com/jstemmer/go-junit-report v0.9.1 // indirect
55-
github.com/klauspost/compress v1.13.0 // indirect
59+
github.com/klauspost/compress v1.15.11 // indirect
5660
github.com/mattn/go-colorable v0.1.1 // indirect
5761
github.com/mattn/go-isatty v0.0.5 // indirect
5862
github.com/mattn/go-zglob v0.0.2-0.20190814121620-e3c945676326 // indirect
5963
github.com/mitchellh/cli v1.0.0 // indirect
6064
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
6165
github.com/mitchellh/copystructure v1.2.0 // indirect
6266
github.com/mitchellh/go-homedir v1.1.0 // indirect
63-
github.com/mitchellh/go-testing-interface v1.0.0 // indirect
67+
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
6468
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
6569
github.com/mitchellh/hashstructure v1.0.0 // indirect
6670
github.com/mitchellh/mapstructure v1.1.2 // indirect
@@ -69,24 +73,24 @@ require (
6973
github.com/pmezard/go-difflib v1.0.0 // indirect
7074
github.com/posener/complete v1.2.1 // indirect
7175
github.com/spf13/afero v1.2.1 // indirect
72-
github.com/stretchr/testify v1.7.0 // indirect
76+
github.com/stretchr/testify v1.8.1 // indirect
7377
github.com/tmccombs/hcl2json v0.3.3 // indirect
74-
github.com/ulikunitz/xz v0.5.8 // indirect
78+
github.com/ulikunitz/xz v0.5.10 // indirect
7579
github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect
7680
github.com/vmihailenco/tagparser v0.1.1 // indirect
7781
github.com/zclconf/go-cty v1.9.1 // indirect
7882
github.com/zclconf/go-cty-yaml v1.0.1 // indirect
79-
go.opencensus.io v0.23.0 // indirect
80-
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
83+
go.opencensus.io v0.24.0 // indirect
84+
golang.org/x/crypto v0.1.0 // indirect
8185
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
82-
golang.org/x/mod v0.4.2 // indirect
83-
golang.org/x/net v0.0.0-20210614182718-04defd469f4e // indirect
84-
golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b // indirect
85-
golang.org/x/text v0.3.6 // indirect
86-
golang.org/x/tools v0.1.2 // indirect
87-
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
86+
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
87+
golang.org/x/net v0.7.0 // indirect
88+
golang.org/x/sys v0.5.0 // indirect
89+
golang.org/x/text v0.7.0 // indirect
90+
golang.org/x/tools v0.1.12 // indirect
91+
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
8892
google.golang.org/appengine v1.6.7 // indirect
89-
google.golang.org/grpc v1.38.0 // indirect
90-
google.golang.org/protobuf v1.26.0 // indirect
91-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
93+
google.golang.org/grpc v1.51.0 // indirect
94+
google.golang.org/protobuf v1.28.1 // indirect
95+
gopkg.in/yaml.v3 v3.0.1 // indirect
9296
)

0 commit comments

Comments
 (0)