Skip to content

Commit

Permalink
feat: remove GetKafkaInstanceByName function and related code
Browse files Browse the repository at this point in the history
  • Loading branch information
Gezi-lzq committed Aug 8, 2024
1 parent 719cd3a commit c1b45eb
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 48 deletions.
31 changes: 0 additions & 31 deletions client/kafka_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,37 +45,6 @@ func (c *Client) GetKafkaInstance(instanceId string) (*KafkaInstanceResponse, er
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)
}

func (c *Client) DeleteKafkaInstance(instanceId string) error {
req, err := http.NewRequest("DELETE", c.HostURL+instancePath+"/"+instanceId, nil)
if err != nil {
Expand Down
14 changes: 14 additions & 0 deletions client/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,17 @@ type Metric struct {
DisplayName string `json:"displayName"`
Value int `json:"value"`
}

// TopicCreateParam struct for TopicCreateParam
type TopicCreateParam struct {
Name *string `json:"name,omitempty" validate:"regexp=^[a-zA-Z0-9][.a-zA-Z0-9_-]*[a-zA-Z0-9]$"`
Partition int32 `json:"partition"`
CompactStrategy string `json:"compactStrategy"`
Configs []ConfigItemParam `json:"configs,omitempty"`
}

// ConfigItemParam struct for ConfigItemParam
type ConfigItemParam struct {
Key *string `json:"key,omitempty"`
Value map[string]interface{} `json:"value,omitempty"`
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ require (
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform-exec v0.21.0 // indirect
github.com/hashicorp/terraform-json v0.22.1 // indirect
github.com/hashicorp/terraform-plugin-framework-validators v0.13.0
github.com/hashicorp/terraform-plugin-framework-timeouts v0.4.1
github.com/hashicorp/terraform-plugin-framework-timetypes v0.5.0
github.com/hashicorp/terraform-plugin-framework-validators v0.13.0
github.com/hashicorp/terraform-registry-address v0.2.3 // indirect
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/automq_kafka_instance_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ func (r *KafkaInstanceResource) Delete(ctx context.Context, req resource.DeleteR
}

func GetKafkaInstance(instance *KafkaInstanceResourceModel, client *client.Client) (*client.KafkaInstanceResponse, error) {
kafka, err := client.GetKafkaInstanceByName(instance.Name.ValueString())
kafka, err := client.GetKafkaInstance(instance.InstanceID.ValueString())
if err != nil {
return nil, fmt.Errorf("error getting Kafka instance by name %s: %v", instance.Name.ValueString(), err)
}
Expand Down
48 changes: 33 additions & 15 deletions internal/provider/automq_kafka_instance_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"testing"
"time"

"terraform-provider-automq/client"

Expand Down Expand Up @@ -114,27 +115,44 @@ resource "automq_kafka_instance" "test" {

// Return a json string for a KafkaInstanceResponse with Creating status
func testAccKafkaInstanceResponseInCreating() client.KafkaInstanceResponse {
createInstanceResponse := client.KafkaInstanceResponse{}
createInstanceResponse.Status = stateCreating
createInstanceResponse.DisplayName = "test"
createInstanceResponse.InstanceID = "test"
return createInstanceResponse
instanceResponse := newInstanceResponse()

instanceResponse.Status = stateCreating
instanceResponse.GmtCreate = time.Now()
instanceResponse.GmtModified = time.Now()
return instanceResponse
}

// Return a json string for a KafkaInstanceResponse with Available status
func testAccKafkaInstanceResponseInAvailable() client.KafkaInstanceResponse {
createInstanceResponse := client.KafkaInstanceResponse{}
createInstanceResponse.Status = stateAvailable
createInstanceResponse.DisplayName = "test"
createInstanceResponse.InstanceID = "test"
return createInstanceResponse
instanceResponse := newInstanceResponse()

instanceResponse.Status = stateAvailable
instanceResponse.GmtModified = time.Now()
return instanceResponse
}

// Return a json string for a KafkaInstanceResponse with Available status
func testAccKafkaInstanceResponseInDeleting() client.KafkaInstanceResponse {
createInstanceResponse := client.KafkaInstanceResponse{}
createInstanceResponse.Status = stateDeleting
createInstanceResponse.DisplayName = "test"
createInstanceResponse.InstanceID = "test"
return createInstanceResponse
instanceResponse := newInstanceResponse()

instanceResponse.Status = stateDeleting
instanceResponse.GmtModified = time.Now()
return instanceResponse
}

func newInstanceResponse() client.KafkaInstanceResponse {
instanceResponse := client.KafkaInstanceResponse{}
instanceResponse.InstanceID = "kf-cakz90r71mspc7vy"
instanceResponse.DisplayName = "test"
instanceResponse.Description = "test"
instanceResponse.Provider = "aliyun"
instanceResponse.Region = "cn-hangzhou"
instanceResponse.Spec.Version = "1.2.0"
instanceResponse.Spec.PaymentPlan.PaymentType = "ON_DEMAND"
instanceResponse.Spec.PaymentPlan.Period = 1
instanceResponse.Spec.PaymentPlan.Unit = "MONTH"
instanceResponse.Spec.Values = []client.Value{{Key: "aku", Value: 6}}
instanceResponse.Networks = []client.Network{{Zone: "cn-hangzhou-b", Subnets: []client.Subnet{{Subnet: "vsw-bp14v5eikr8wrgoqje7hr"}}}}
return instanceResponse
}

0 comments on commit c1b45eb

Please sign in to comment.