Skip to content

Commit

Permalink
Fix: Add external wrapper around JSON response for Cluster.GetBySlug
Browse files Browse the repository at this point in the history
  • Loading branch information
momer committed May 15, 2024
1 parent 8aaffe3 commit c84d47c
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 59 deletions.
21 changes: 12 additions & 9 deletions bonsai/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ type Cluster struct {
State ClusterState `json:"state"`
}

type ClusterResultGetBySlug struct {
Cluster `json:"cluster"`
}

// ClustersResultList is a wrapper around a slice of
// Clusters for json unmarshaling.
type ClustersResultList struct {
Expand Down Expand Up @@ -311,15 +315,14 @@ func (c *ClusterClient) All(ctx context.Context) ([]Cluster, error) {
}

// GetBySlug gets a Cluster from the Clusters API by its slug.
//
//nolint:dupl // Allow duplicated code blocks in code paths that may change
func (c *ClusterClient) GetBySlug(ctx context.Context, slug string) (Cluster, error) {
var (
req *http.Request
reqURL *url.URL
resp *Response
err error
result Cluster
req *http.Request
reqURL *url.URL
resp *Response
err error
result Cluster
intermediaryResult ClusterResultGetBySlug
)

reqURL, err = url.Parse(ClusterAPIBasePath)
Expand All @@ -339,11 +342,11 @@ func (c *ClusterClient) GetBySlug(ctx context.Context, slug string) (Cluster, er
return result, fmt.Errorf("client.do failed: %w", err)
}

if err = json.Unmarshal(resp.BodyBuf.Bytes(), &result); err != nil {
if err = json.Unmarshal(resp.BodyBuf.Bytes(), &intermediaryResult); err != nil {
return result, fmt.Errorf("json.Unmarshal failed: %w", err)
}

return result, nil
return intermediaryResult.Cluster, nil
}

// Create requests a new Cluster to be created.
Expand Down
64 changes: 33 additions & 31 deletions bonsai/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,40 +255,42 @@ func (s *ClientMockTestSuite) TestClusterClient_GetBySlug() {
s.serveMux.Get(urlPath, func(w http.ResponseWriter, _ *http.Request) {
respStr := fmt.Sprintf(`
{
"slug": "%s",
"name": "second_testing_cluster",
"uri": "https://api.bonsai.io/clusters/second-testing-clust-1234567890",
"plan": {
"slug": "sandbox-aws-us-east-1",
"uri": "https://api.bonsai.io/plans/sandbox-aws-us-east-1"
},
"release": {
"version": "7.2.0",
"slug": "elasticsearch-7.2.0",
"package_name": "7.2.0",
"service_type": "elasticsearch",
"uri": "https://api.bonsai.io/releases/elasticsearch-7.2.0"
},
"space": {
"path": "omc/bonsai/us-east-1/common",
"region": "aws-us-east-1",
"uri": "https://api.bonsai.io/spaces/omc/bonsai/us-east-1/common"
},
"stats": {
"docs": 0,
"shards_used": 0,
"data_bytes_used": 0
},
"access": {
"host": "second-testing-clust-1234567890.us-east-1.bonsaisearch.net",
"port": 443,
"scheme": "https"
},
"state": "PROVISIONED"
"cluster": {
"slug": "%s",
"name": "second_testing_cluster",
"uri": "https://api.bonsai.io/clusters/second-testing-clust-1234567890",
"plan": {
"slug": "sandbox-aws-us-east-1",
"uri": "https://api.bonsai.io/plans/sandbox-aws-us-east-1"
},
"release": {
"version": "7.2.0",
"slug": "elasticsearch-7.2.0",
"package_name": "7.2.0",
"service_type": "elasticsearch",
"uri": "https://api.bonsai.io/releases/elasticsearch-7.2.0"
},
"space": {
"path": "omc/bonsai/us-east-1/common",
"region": "aws-us-east-1",
"uri": "https://api.bonsai.io/spaces/omc/bonsai/us-east-1/common"
},
"stats": {
"docs": 0,
"shards_used": 0,
"data_bytes_used": 0
},
"access": {
"host": "second-testing-clust-1234567890.us-east-1.bonsaisearch.net",
"port": 443,
"scheme": "https"
},
"state": "PROVISIONED"
}
}
`, targetClusterSlug)

resp := &bonsai.Cluster{}
resp := &bonsai.ClusterResultGetBySlug{}
err = json.Unmarshal([]byte(respStr), resp)
s.NoError(err, "unmarshals json into bonsai.Space")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
{
"slug": "",
"name": "",
"uri": "",
"slug": "dcek-group-llc-5240651189",
"name": "DCEK Group, LLC search",
"uri": "https://api.bonsai.io/clusters/dcek-group-llc-5240651189",
"plan": {
"slug": "",
"available_releases": null,
"available_spaces": null
"slug": "sandbox-aws-us-east-1",
"available_releases": [],
"available_spaces": [],
"uri": "https://api.bonsai.io/plans/sandbox-aws-us-east-1"
},
"release": {
"slug": "elasticsearch-7.10.2",
"service_type": "elasticsearch",
"version": "7.10.2",
"uri": "https://api.bonsai.io/releases/elasticsearch-7.10.2",
"package_name": "7.10.2"
},
"release": {},
"space": {
"path": "",
"path": "omc/bonsai/us-east-1/common",
"private_network": false,
"cloud": {
"provider": "",
"region": ""
}
},
"region": "aws-us-east-1",
"uri": "https://api.bonsai.io/spaces/omc/bonsai/us-east-1/common"
},
"stats": {
"docs": 2,
"shards_used": 2,
"data_bytes_used": 17984
},
"stats": {},
"access": {
"host": "",
"port": 0,
"scheme": ""
"host": "dcek-group-llc-5240651189.us-east-1.bonsaisearch.net",
"port": 443,
"scheme": "https"
},
"state": ""
"state": "PROVISIONED"
}
10 changes: 5 additions & 5 deletions bonsai/fixtures/vcr/TestClusterClient-GetBySlug.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ interactions:
Content-Type:
- application/json
User-Agent:
- bonsai-api-go/v1 bonsai-api-go/1.0.0
- bonsai-api-go/v2.1.0 bonsai-api-go/v2.1.0
url: https://api.bonsai.io/clusters/dcek-group-llc-5240651189
method: GET
response:
Expand All @@ -43,7 +43,7 @@ interactions:
Content-Type:
- application/json; charset=utf-8
Date:
- Sat, 04 May 2024 11:42:50 GMT
- Wed, 15 May 2024 00:14:38 GMT
Etag:
- W/"c289f3e152b59209b26bcc8f03b2a044"
Referrer-Policy:
Expand All @@ -65,11 +65,11 @@ interactions:
X-Permitted-Cross-Domain-Policies:
- none
X-Request-Id:
- 8f04bb3f-6396-4cdf-8041-04f0ded81abe
- 989e03a8-77ec-4ac4-9dd5-927b8dd88683
X-Runtime:
- "0.039003"
- "0.059638"
X-Xss-Protection:
- 1; mode=block
status: 200 OK
code: 200
duration: 68.2324ms
duration: 337.2444ms

0 comments on commit c84d47c

Please sign in to comment.