Skip to content

Commit

Permalink
refactor: check success by 2xx range
Browse files Browse the repository at this point in the history
  • Loading branch information
uleMeshcloud committed Dec 10, 2024
1 parent 2d35645 commit f31f684
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 33 deletions.
10 changes: 4 additions & 6 deletions client/buildingblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"io"
"net/http"
"net/url"

"github.com/meshcloud/terraform-provider-meshstack/client"
)

const (
Expand Down Expand Up @@ -103,9 +105,7 @@ func (c *MeshStackProviderClient) ReadBuildingBlock(uuid string) (*MeshBuildingB
return nil, nil
}

if res.StatusCode != 200 {
return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data)
}
isSuccessHTTPStatusOrThrow(res)

var bb MeshBuildingBlock
err = json.Unmarshal(data, &bb)
Expand Down Expand Up @@ -141,9 +141,7 @@ func (c *MeshStackProviderClient) CreateBuildingBlock(bb *MeshBuildingBlockCreat
return nil, err
}

if res.StatusCode != 201 {
return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data)
}
isSuccessHTTPStatusOrThrow(res)

var createdBb MeshBuildingBlock
err = json.Unmarshal(data, &createdBb)
Expand Down
10 changes: 4 additions & 6 deletions client/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"io"
"net/http"
"net/url"

"github.com/meshcloud/terraform-provider-meshstack/client"
)

const CONTENT_TYPE_PROJECT = "application/vnd.meshcloud.api.meshproject.v2.hal+json"
Expand Down Expand Up @@ -178,9 +180,7 @@ func (c *MeshStackProviderClient) CreateProject(project *MeshProjectCreate) (*Me
return nil, err
}

if res.StatusCode != 201 {
return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data)
}
isSuccessHTTPStatusOrThrow(res)

var createdProject MeshProject
err = json.Unmarshal(data, &createdProject)
Expand Down Expand Up @@ -219,9 +219,7 @@ func (c *MeshStackProviderClient) UpdateProject(project *MeshProjectCreate) (*Me
return nil, err
}

if res.StatusCode != 200 {
return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data)
}
isSuccessHTTPStatusOrThrow(res)

var updatedProject MeshProject
err = json.Unmarshal(data, &updatedProject)
Expand Down
10 changes: 4 additions & 6 deletions client/project_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"io"
"net/http"
"net/url"

"github.com/meshcloud/terraform-provider-meshstack/client"
)

type MeshProjectBinding struct {
Expand Down Expand Up @@ -70,9 +72,7 @@ func (c *MeshStackProviderClient) readProjectBinding(name string, contentType st
return nil, nil
}

if res.StatusCode != 200 {
return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data)
}
isSuccessHTTPStatusOrThrow(res)

var binding MeshProjectBinding
err = json.Unmarshal(data, &binding)
Expand Down Expand Up @@ -120,9 +120,7 @@ func (c *MeshStackProviderClient) createProjectBinding(binding *MeshProjectBindi
return nil, err
}

if res.StatusCode != 200 {
return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data)
}
isSuccessHTTPStatusOrThrow(res)

var createdBinding MeshProjectBinding
err = json.Unmarshal(data, &createdBinding)
Expand Down
20 changes: 20 additions & 0 deletions client/status_code_checker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package client

import (
"io"
"fmt"
"net/http"
)

func isSuccessHTTPStatusOrThrow(resp *http.Response) error {
data, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}

if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return fmt.Errorf("unexpected status code: got %d, expected 2xx range\nBody: %s", resp.StatusCode, data)
}

return nil
}
14 changes: 5 additions & 9 deletions client/tag_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"io"
"net/http"
"net/url"

"github.com/meshcloud/terraform-provider-meshstack/client"
)

const API_VERSION_TAG_DEFINITION = "v1"
Expand Down Expand Up @@ -156,9 +158,7 @@ func (c *MeshStackProviderClient) ReadTagDefinition(name string) (*MeshTagDefini
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to read tag definition: %s", resp.Status)
}
isSuccessHTTPStatusOrThrow(resp)

var tagDefinition MeshTagDefinition
if err := json.NewDecoder(resp.Body).Decode(&tagDefinition); err != nil {
Expand Down Expand Up @@ -191,9 +191,7 @@ func (c *MeshStackProviderClient) CreateTagDefinition(tagDefinition *MeshTagDefi
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("failed to create tag definition: %s", resp.Status)
}
isSuccessHTTPStatusOrThrow(resp)

var createdTagDefinition MeshTagDefinition
if err := json.NewDecoder(resp.Body).Decode(&createdTagDefinition); err != nil {
Expand Down Expand Up @@ -224,9 +222,7 @@ func (c *MeshStackProviderClient) UpdateTagDefinition(tagDefinition *MeshTagDefi
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to update tag definition: %s", resp.Status)
}
isSuccessHTTPStatusOrThrow(resp)

var updatedTagDefinition MeshTagDefinition
if err := json.NewDecoder(resp.Body).Decode(&updatedTagDefinition); err != nil {
Expand Down
10 changes: 4 additions & 6 deletions client/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"io"
"net/http"
"net/url"

"github.com/meshcloud/terraform-provider-meshstack/client"
)

const CONTENT_TYPE_TENANT = "application/vnd.meshcloud.api.meshtenant.v3.hal+json"
Expand Down Expand Up @@ -83,9 +85,7 @@ func (c *MeshStackProviderClient) ReadTenant(workspace string, project string, p
return nil, nil
}

if res.StatusCode != 200 {
return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data)
}
isSuccessHTTPStatusOrThrow(res)

var tenant MeshTenant
err = json.Unmarshal(data, &tenant)
Expand Down Expand Up @@ -121,9 +121,7 @@ func (c *MeshStackProviderClient) CreateTenant(tenant *MeshTenantCreate) (*MeshT
return nil, err
}

if res.StatusCode != 201 {
return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data)
}
isSuccessHTTPStatusOrThrow(res)

var createdTenant MeshTenant
err = json.Unmarshal(data, &createdTenant)
Expand Down

0 comments on commit f31f684

Please sign in to comment.