diff --git a/client/kafka_instance.go b/client/kafka_instance.go index 98b5131..778deaa 100644 --- a/client/kafka_instance.go +++ b/client/kafka_instance.go @@ -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 { diff --git a/client/models.go b/client/models.go index 0de6029..bb72936 100644 --- a/client/models.go +++ b/client/models.go @@ -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"` +} diff --git a/go.mod b/go.mod index f421d5e..5597edb 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/internal/provider/automq_kafka_instance_resource.go b/internal/provider/automq_kafka_instance_resource.go index e2f1d6d..ea5e1e6 100644 --- a/internal/provider/automq_kafka_instance_resource.go +++ b/internal/provider/automq_kafka_instance_resource.go @@ -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) } diff --git a/internal/provider/automq_kafka_instance_resource_test.go b/internal/provider/automq_kafka_instance_resource_test.go index 21e1609..c334189 100644 --- a/internal/provider/automq_kafka_instance_resource_test.go +++ b/internal/provider/automq_kafka_instance_resource_test.go @@ -6,6 +6,7 @@ import ( "fmt" "net/http" "testing" + "time" "terraform-provider-automq/client" @@ -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 }