Skip to content

Commit

Permalink
apiResponse nil checks and logic fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Florin Mihalache committed May 20, 2021
1 parent 9b569ba commit e6f85d3
Show file tree
Hide file tree
Showing 437 changed files with 17,287 additions and 14,362 deletions.
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ require (
github.com/agext/levenshtein v1.2.3 // indirect
github.com/fatih/color v1.9.0 // indirect
github.com/go-test/deep v1.0.6 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/hashicorp/go-hclog v0.14.1 // indirect
github.com/hashicorp/go-multierror v1.1.0 // indirect
github.com/hashicorp/go-uuid v1.0.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/terraform-config-inspect v0.0.0-20200526195750-d43f12b82861 // indirect
github.com/hashicorp/terraform-plugin-sdk v1.16.1
github.com/hashicorp/yamux v0.0.0-20200609203250-aecfd211c9ce // indirect
github.com/ionos-cloud/sdk-go/v5 v5.1.0-beta.1.0.20210517153216-0d6b62346c81
github.com/ionos-cloud/sdk-go/v5 v5.1.0
github.com/jhump/protoreflect v1.6.1 // indirect
github.com/kr/pretty v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.7 // indirect
Expand All @@ -24,4 +25,7 @@ require (
github.com/profitbricks/profitbricks-sdk-go/v5 v5.0.32
github.com/spf13/afero v1.3.1 // indirect
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
golang.org/x/net v0.0.0-20210510120150-4163338589ed // indirect
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c // indirect
google.golang.org/appengine v1.6.7 // indirect
)
24 changes: 22 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvq
github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8=
github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0=
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
Expand All @@ -145,6 +148,8 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/martian v2.1.0+incompatible h1:/CP5g8u/VJHijgedC/Legn3BAbAaWPgecwXBIDzw5no=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/martian/v3 v3.0.0 h1:pMen7vLs8nvgEYhywH3KDWJIJTeEr2ULsVWHWYHQyBs=
Expand Down Expand Up @@ -221,8 +226,8 @@ github.com/hashicorp/yamux v0.0.0-20200609203250-aecfd211c9ce/go.mod h1:+NfK9FKe
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/imdario/mergo v0.3.9 h1:UauaLniWCFHWd+Jp9oCEkTBj8VO/9DKg3PV3VCNMDIg=
github.com/imdario/mergo v0.3.9/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/ionos-cloud/sdk-go/v5 v5.1.0-beta.1.0.20210517153216-0d6b62346c81 h1:djoTx0bp0CXUqqNzvrm2MF3mM7KuVWO3jAyWCpEB3v4=
github.com/ionos-cloud/sdk-go/v5 v5.1.0-beta.1.0.20210517153216-0d6b62346c81/go.mod h1:dU3BVZgiBDAKyo41hFk7D5KWHfqyp4YL4rePIAi2JyM=
github.com/ionos-cloud/sdk-go/v5 v5.1.0 h1:q6KWvirQ2td8f+w3sltWzQ7w1Sl9TbiSOcFRdarfSPo=
github.com/ionos-cloud/sdk-go/v5 v5.1.0/go.mod h1:dU3BVZgiBDAKyo41hFk7D5KWHfqyp4YL4rePIAi2JyM=
github.com/jarcoal/httpmock v1.0.5 h1:cHtVEcTxRSX4J0je7mWPfc9BpDpqzXSJ5HbymZmyHck=
github.com/jarcoal/httpmock v1.0.5/go.mod h1:ATjnClrvW/3tijVmpL/va5Z3aAyGvqU3gCT8nX0Txik=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
Expand Down Expand Up @@ -431,13 +436,17 @@ golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81R
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b h1:uwuIcX0g4Yl1NC5XAz37xsr2lTtcqevgzYNVt49waME=
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210510120150-4163338589ed h1:p9UgmWI9wKpfYmgaV/IZKGdXc5qEK45tDwwwDyjS26I=
golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43 h1:ld7aEMNHoBnnDAX15v1T6z31v8HwR2A9FYOuAhWqkwc=
golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c h1:pkQiBZBvdos9qq4wBAHqlzuZHEXo07pqV06ef90u1WI=
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down Expand Up @@ -482,12 +491,18 @@ golang.org/x/sys v0.0.0-20200803210538-64077c9b5642/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200905004654-be1d3432aa8f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f h1:+Nyd8tzPX9R7BWHguqsrbFdRx3WQ/1ib8I44HXV5yTA=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da h1:b3NXsE2LusjYGGjL5bxEVZZORm/YEFFrWFjR8eFrw/c=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down Expand Up @@ -565,6 +580,8 @@ google.golang.org/appengine v1.6.1/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww
google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
google.golang.org/appengine v1.6.6 h1:lMO5rYAqUxkmaj76jAkRUvt5JZgFymx/+Q5Mzfivuhc=
google.golang.org/appengine v1.6.6/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
google.golang.org/genproto v0.0.0-20170818010345-ee236bd376b0/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
Expand Down Expand Up @@ -623,6 +640,9 @@ google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpAD
google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGjtUeSXeh4=
google.golang.org/protobuf v1.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c=
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
36 changes: 12 additions & 24 deletions ionoscloud/resource_backup_unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,10 @@ func resourceBackupUnitRead(d *schema.ResourceData, meta interface{}) error {
backupUnit, apiResponse, err := client.BackupUnitApi.BackupunitsFindById(ctx, d.Id()).Execute()

if err != nil {
if _, ok := err.(ionoscloud.GenericOpenAPIError); ok {
if apiResponse.Response.StatusCode == 404 {
d.SetId("")
return nil
}
if apiResponse != nil && apiResponse.Response.StatusCode == 404 {
d.SetId("")
return nil
}

return fmt.Errorf("Error while fetching backup unit %s: %s", d.Id(), err)
}

Expand Down Expand Up @@ -192,12 +189,9 @@ func resourceBackupUnitUpdate(d *schema.ResourceData, meta interface{}) error {
_, apiResponse, err := client.BackupUnitApi.BackupunitsPut(ctx, d.Id()).BackupUnit(request).Execute()

if err != nil {
if _, ok := err.(ionoscloud.GenericOpenAPIError); ok {
if apiResponse.Response.StatusCode == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("Error while updating backup unit: %s", err)
if apiResponse != nil && apiResponse.Response.StatusCode == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("Error while updating backup unit %s: %s", d.Id(), err)
}
Expand Down Expand Up @@ -232,14 +226,10 @@ func resourceBackupUnitDelete(d *schema.ResourceData, meta interface{}) error {
_, apiResponse, err := client.BackupUnitApi.BackupunitsDelete(ctx, d.Id()).Execute()

if err != nil {
if _, ok := err.(ionoscloud.GenericOpenAPIError); ok {
if apiResponse.Response.StatusCode == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("Error while deleting backup unit: %s", err)
if apiResponse != nil && apiResponse.Response.StatusCode == 404 {
d.SetId("")
return nil
}

return fmt.Errorf("Error while deleting backup unit %s: %s", d.Id(), err)
}

Expand Down Expand Up @@ -275,12 +265,10 @@ func backupUnitDeleted(client *ionoscloud.APIClient, d *schema.ResourceData, c c
_, apiResponse, err := client.BackupUnitApi.BackupunitsFindById(c, d.Id()).Execute()

if err != nil {
if _, ok := err.(ionoscloud.GenericOpenAPIError); ok {
if apiResponse.Response.StatusCode == 404 {
return true, nil
}
return true, fmt.Errorf("Error checking backup unit deletion status: %s", err)
if apiResponse != nil && apiResponse.Response.StatusCode == 404 {
return true, nil
}
return true, fmt.Errorf("error checking backup unit deletion status: %s", err)
}
return false, nil
}
16 changes: 9 additions & 7 deletions ionoscloud/resource_backup_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,23 @@ func TestAccbackupUnit_Basic(t *testing.T) {
func testAccCheckbackupUnitDestroyCheck(s *terraform.State) error {
client := testAccProvider.Meta().(*ionoscloud.APIClient)

ctx, cancel := context.WithTimeout(context.Background(), *resourceDefaultTimeouts.Default)

if cancel != nil {
defer cancel()
}


for _, rs := range s.RootModule().Resources {

if rs.Type != "ionoscloud_backup_unit" {
continue
}

ctx, cancel := context.WithTimeout(context.Background(), *resourceDefaultTimeouts.Default)

if cancel != nil {
defer cancel()
}

_, apiResponse, err := client.BackupUnitApi.BackupunitsFindById(ctx, rs.Primary.ID).Execute()

if _, ok := err.(ionoscloud.GenericOpenAPIError); ok {
if apiResponse.Response.StatusCode != 404 {
if apiResponse != nil && apiResponse.Response.StatusCode != 404 {
return fmt.Errorf("backup unit still exists %s %s", rs.Primary.ID, string(apiResponse.Payload))
}
} else {
Expand Down
10 changes: 4 additions & 6 deletions ionoscloud/resource_datacenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,11 @@ func resourceDatacenterRead(d *schema.ResourceData, meta interface{}) error {
datacenter, apiResponse, err := client.DataCenterApi.DatacentersFindById(ctx, d.Id()).Execute()

if err != nil {
if _, ok := err.(ionoscloud.GenericOpenAPIError); ok {
if apiResponse.Response.StatusCode == 404 {
d.SetId("")
return nil
}
if apiResponse != nil && apiResponse.Response.StatusCode == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("Error while fetching a data center ID %s %s", d.Id(), err)
return fmt.Errorf("error while fetching a data center ID %s %s", d.Id(), err)
}

if datacenter.Properties.Name != nil {
Expand Down
8 changes: 6 additions & 2 deletions ionoscloud/resource_datacenter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ func testAccCheckDatacenterDestroyCheck(s *terraform.State) error {
_, apiResponse, err := client.DataCenterApi.DatacentersFindById(ctx, rs.Primary.ID).Execute()

if _, ok := err.(ionoscloud.GenericOpenAPIError); ok {
if apiResponse.Response.StatusCode != 404 {
return fmt.Errorf("DataCenter still exists %s %s", rs.Primary.ID, string(apiResponse.Payload))
if apiResponse == nil || apiResponse.Response.StatusCode != 404 {
var payload = "<nil>"
if apiResponse != nil {
payload = string(apiResponse.Payload)
}
return fmt.Errorf("DataCenter still exists %s %s", rs.Primary.ID, payload)
}
} else {
return fmt.Errorf("Unable to fetching DataCenter %s %s", rs.Primary.ID, err)
Expand Down
8 changes: 3 additions & 5 deletions ionoscloud/resource_firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,9 @@ func resourceFirewallRead(d *schema.ResourceData, meta interface{}) error {
d.Get("server_id").(string), d.Get("nic_id").(string), d.Id()).Execute()

if err != nil {
if _, ok := err.(ionoscloud.GenericOpenAPIError); ok {
if apiResponse.Response.StatusCode == 404 {
d.SetId("")
return nil
}
if apiResponse != nil && apiResponse.Response.StatusCode == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("An error occured while fetching a firewall rule dcId: %s server_id: %s nic_id: %s ID: %s %s", d.Get("datacenter_id").(string), d.Get("server_id").(string), d.Get("nic_id").(string), d.Id(), err)
}
Expand Down
15 changes: 6 additions & 9 deletions ionoscloud/resource_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/profitbricks/profitbricks-sdk-go/v5"
)

func resourceGroup() *schema.Resource {
Expand Down Expand Up @@ -184,14 +183,12 @@ func resourceGroupRead(d *schema.ResourceData, meta interface{}) error {
defer cancel()
}

group, _, err := client.UserManagementApi.UmGroupsFindById(ctx, d.Id()).Execute()
group, apiResponse, err := client.UserManagementApi.UmGroupsFindById(ctx, d.Id()).Execute()

if err != nil {
if apiError, ok := err.(profitbricks.ApiError); ok {
if apiError.HttpStatusCode() == 404 {
d.SetId("")
return nil
}
if apiResponse != nil && apiResponse.Response.StatusCode == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("An error occured while fetching a Group ID %s %s", d.Id(), err)
}
Expand Down Expand Up @@ -366,8 +363,8 @@ func resourceGroupDelete(d *schema.ResourceData, meta interface{}) error {

if err != nil {
if _, ok := err.(ionoscloud.GenericOpenAPIError); ok {
if apiResponse.Response.StatusCode != 404 {
return fmt.Errorf("An error occured while deleting a group %s %s", d.Id(), err)
if apiResponse == nil || apiResponse.Response.StatusCode != 404 {
return fmt.Errorf("an error occured while deleting a group %s %s", d.Id(), err)
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions ionoscloud/resource_ipblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,11 @@ func resourceIPBlockRead(d *schema.ResourceData, meta interface{}) error {
ipBlock, apiResponse, err := client.IPBlocksApi.IpblocksFindById(ctx, d.Id()).Execute()

if err != nil {
if _, ok := err.(ionoscloud.GenericOpenAPIError); ok {
if apiResponse.Response.StatusCode == 404 {
d.SetId("")
return nil
}
if apiResponse != nil && apiResponse.Response.StatusCode == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("An error occured while fetching an ip block ID %s %s", d.Id(), err)
return fmt.Errorf("an error occured while fetching an ip block ID %s %s", d.Id(), err)
}

log.Printf("[INFO] IPS: %s", strings.Join(*ipBlock.Properties.Ips, ","))
Expand Down
2 changes: 1 addition & 1 deletion ionoscloud/resource_ipblock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func testAccCheckIPBlockDestroyCheck(s *terraform.State) error {
_, apiResponse, err := client.IPBlocksApi.IpblocksFindById(ctx, rs.Primary.ID).Execute()

if apiError, ok := err.(ionoscloud.GenericOpenAPIError); ok {
if apiResponse.Response.StatusCode != 404 {
if apiResponse != nil && apiResponse.Response.StatusCode != 404 {
return fmt.Errorf("IPBlock still exists %s %s", rs.Primary.ID, apiError)
}
} else {
Expand Down
18 changes: 6 additions & 12 deletions ionoscloud/resource_ipfailover.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,11 @@ func resourceLanIPFailoverRead(d *schema.ResourceData, meta interface{}) error {
lan, apiResponse, err := client.LanApi.DatacentersLansFindById(ctx, d.Get("datacenter_id").(string), d.Id()).Execute()

if err != nil {
if _, ok := err.(ionoscloud.GenericOpenAPIError); ok {
if apiResponse.Response.StatusCode == 404 {
d.SetId("")
return nil
}
if apiResponse != nil && apiResponse.Response.StatusCode == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("An error occured while fetching a lan ID %s %s", d.Id(), err)
return fmt.Errorf("an error occured while fetching a lan ID %s %s", d.Id(), err)
}

if lan.Properties.IpFailover != nil {
Expand Down Expand Up @@ -184,12 +182,8 @@ func resourceLanIPFailoverDelete(d *schema.ResourceData, meta interface{}) error
time.Sleep(90 * time.Second)
_, apiResponse, err = client.LanApi.DatacentersLansPatch(ctx, dcid, lanid).Lan(*properties).Execute()

if err != nil {
if _, ok := err.(ionoscloud.GenericOpenAPIError); ok {
if apiResponse.Response.StatusCode != 404 {
return fmt.Errorf("An error occured while removing a lans ipfailover groups dcId %s ID %s %s", d.Get("datacenter_id").(string), d.Id(), err)
}
}
if err != nil && (apiResponse == nil || apiResponse.Response.StatusCode != 404) {
return fmt.Errorf("an error occured while removing a lans ipfailover groups dcId %s ID %s %s", d.Get("datacenter_id").(string), d.Id(), err)
}
}

Expand Down
Loading

0 comments on commit e6f85d3

Please sign in to comment.