Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update client package to include error handling in NewClient fu… #5

Merged
merged 7 commits into from
Aug 7, 2024
Merged
20 changes: 15 additions & 5 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ type AuthStruct struct {
SecretKey string `json:"secret_key"`
}

type ErrorResponse struct {
Code int `json:"code"`
ErrorMessage string `json:"error_message"`
Err error `json:"error"`
}

func (e *ErrorResponse) Error() string {
return fmt.Errorf("code: %d, message: %s, details: %v", e.Code, e.ErrorMessage, e.Err).Error()
}

func NewClient(host, token *string) (*Client, error) {
c := Client{
HTTPClient: &http.Client{Timeout: 10 * time.Second},
Expand Down Expand Up @@ -59,18 +69,18 @@ func (c *Client) doRequest(req *http.Request, authToken *string) ([]byte, error)

res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
return nil, &ErrorResponse{Code: 0, ErrorMessage: "Error sending request", Err: err}
}
defer res.Body.Close()

body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
return nil, &ErrorResponse{Code: res.StatusCode, ErrorMessage: "Error reading response body", Err: err}
}

if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("status: %d, body: %s", res.StatusCode, body)
if res.StatusCode < 200 || res.StatusCode >= 300 {
return nil, &ErrorResponse{Code: res.StatusCode, ErrorMessage: string(body), Err: nil}
}

return body, err
return body, nil
}
57 changes: 49 additions & 8 deletions client/kafka_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"encoding/json"
"fmt"
"net/http"
"strings"
)
Expand All @@ -15,37 +16,77 @@ func (c *Client) CreateKafkaInstance(kafka KafkaInstanceRequest) (*KafkaInstance
if err != nil {
return nil, err
}

body, err := c.doRequest(req, &c.Token)
if err != nil {
return nil, err
}

newkafka := KafkaInstanceResponse{}
err = json.Unmarshal(body, &newkafka)
if err != nil {
return nil, err
}

return &newkafka, nil
}

func (c *Client) GetKafkaInstance(id string) (*KafkaInstanceResponse, error) {
req, err := http.NewRequest("GET", c.HostURL+"/api/v1/instances/"+id, nil)
func (c *Client) GetKafkaInstance(instanceId string) (*KafkaInstanceResponse, error) {
req, err := http.NewRequest("GET", c.HostURL+"/api/v1/instances/"+instanceId, nil)
if err != nil {
return nil, err
return nil, fmt.Errorf("error creating request: %v", err)
}
body, err := c.doRequest(req, &c.Token)
if err != nil {
if err.(*ErrorResponse).Code == 404 {
return nil, nil
}
return nil, fmt.Errorf("error doing request: %v", err)
}
instance := KafkaInstanceResponse{}
err = json.Unmarshal(body, &instance)
if err != nil {
return nil, fmt.Errorf("error unmarshaling response: %v", err)
}
return &instance, nil
}

func (c *Client) GetKafkaInstanceByName(name string) (*KafkaInstanceResponse, error) {
req, err := http.NewRequest("GET", c.HostURL+instancePath+"?keyword="+name, nil)
if err != nil {
return nil, err
}
body, err := c.doRequest(req, &c.Token)
if err != nil {
return nil, err
}

kafka := KafkaInstanceResponse{}
err = json.Unmarshal(body, &kafka)
if err != nil {
return nil, err
}
klist := KafkaInstanceResponseList{}

err = json.Unmarshal(body, &klist)
if err != nil {
return nil, err
}

var result KafkaInstanceResponse
for _, instance := range klist.List {
if instance.DisplayName == name {
result = instance
return &result, nil
}
}
return nil, fmt.Errorf("Kafka instance with name %s not found", name)
}

return &kafka, nil
func (c *Client) DeleteKafkaInstance(instanceId string) error {
req, err := http.NewRequest("DELETE", c.HostURL+instancePath+"/"+instanceId, nil)
if err != nil {
return err
}
_, err = c.doRequest(req, &c.Token)
if err != nil {
return err
}
return nil
}
89 changes: 57 additions & 32 deletions client/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type KafkaInstanceRequest struct {
}

type KafkaInstanceRequestSpec struct {
Version string `json:"version"`
Template string `json:"template"`
PaymentPlan KafkaInstanceRequestPaymentPlan `json:"paymentPlan"`
Values []KafkaInstanceRequestValues `json:"values"`
Expand All @@ -34,39 +35,63 @@ type KafkaInstanceRequestNetwork struct {
}

type KafkaInstanceResponse struct {
InstanceID string `json:"instanceId"`
GmtCreate time.Time `json:"gmtCreate"`
GmtModified time.Time `json:"gmtModified"`
DisplayName string `json:"displayName"`
Description string `json:"description"`
Status string `json:"status"`
Provider string `json:"provider"`
Region string `json:"region"`
Spec struct {
SpecID string `json:"specId"`
DisplayName string `json:"displayName"`
PaymentPlan struct {
PaymentType string `json:"paymentType"`
Unit string `json:"unit"`
Period int `json:"period"`
} `json:"paymentPlan"`
Template string `json:"template"`
Version string `json:"version"`
Values []struct {
Key string `json:"key"`
Name string `json:"name"`
Value int `json:"value"`
DisplayValue string `json:"displayValue"`
} `json:"values"`
} `json:"spec"`
Networks []struct {
Zone string `json:"zone"`
Subnets []struct {
Subnet string `json:"subnet"`
SubnetName string `json:"subnetName"`
} `json:"subnets"`
} `json:"networks"`
InstanceID string `json:"instanceId"`
GmtCreate time.Time `json:"gmtCreate"`
GmtModified time.Time `json:"gmtModified"`
DisplayName string `json:"displayName"`
Description string `json:"description"`
Status string `json:"status"`
Provider string `json:"provider"`
Region string `json:"region"`
Spec Spec `json:"spec"`
Networks []Network `json:"networks"`
Metrics []interface{} `json:"metrics"`
AclSupported bool `json:"aclSupported"`
AclEnabled bool `json:"aclEnabled"`
}

type Spec struct {
SpecID string `json:"specId"`
DisplayName string `json:"displayName"`
PaymentPlan PaymentPlan `json:"paymentPlan"`
Template string `json:"template"`
Version string `json:"version"`
Values []Value `json:"values"`
}

type PaymentPlan struct {
PaymentType string `json:"paymentType"`
Unit string `json:"unit"`
Period int `json:"period"`
}

type Value struct {
Key string `json:"key"`
Name string `json:"name"`
Value int `json:"value"`
DisplayValue string `json:"displayValue"`
}

type Network struct {
Zone string `json:"zone"`
Subnets []Subnet `json:"subnets"`
}

type Subnet struct {
Subnet string `json:"subnet"`
SubnetName string `json:"subnetName"`
}

type KafkaInstanceResponseList struct {
PageNum int `json:"pageNum"`
PageSize int `json:"pageSize"`
Total int `json:"total"`
List []KafkaInstanceResponse `json:"list"`
TotalPage int `json:"totalPage"`
}

type Metric struct {
Name string `json:"name"`
DisplayName string `json:"displayName"`
Value int `json:"value"`
}
5 changes: 5 additions & 0 deletions client/path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package client

const (
instancePath = "/api/v1/instances"
)
6 changes: 3 additions & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ provider "scaffolding" {

### Optional

- `access_key` (String) Example provider attribute
- `host` (String) Example provider attribute
- `secret_key` (String) Example provider attribute
- `byoc_access_key` (String) Example provider attribute
- `byoc_host` (String) Example provider attribute
- `byoc_secret_key` (String) Example provider attribute
- `token` (String) Example provider attribute
6 changes: 5 additions & 1 deletion docs/resources/kafka_instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ AutoMQ Kafka instance resource

- `cloud_provider` (String) The cloud provider of the Kafka instance
- `compute_specs` (Attributes) The compute specs of the Kafka instance (see [below for nested schema](#nestedatt--compute_specs))
- `display_name` (String) The display name of the Kafka instance
- `name` (String) The name of the Kafka instance
- `network_type` (String) The network type of the Kafka instance
- `networks` (Attributes List) The networks of the Kafka instance (see [below for nested schema](#nestedatt--networks))
- `region` (String) The region of the Kafka instance
Expand All @@ -39,6 +39,10 @@ Required:

- `aku` (Number) The template of the compute specs

Optional:

- `version` (String) The version of the compute specs


<a id="nestedatt--networks"></a>
### Nested Schema for `networks`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ terraform {
}

provider "automq" {
host = "http://localhost:8081"
token = "123456"
byoc_host = "http://localhost:8081"
token = "123456"
}

resource "automq_kafka_instance" "example" {
display_name = "example"
name = "example"
description = "example"
cloud_provider = "aliyun"
region = "cn-hangzhou"
Expand Down
Loading