From 8118bc5b0ff722e95a9cc9f47b3bcf80b0f2e240 Mon Sep 17 00:00:00 2001 From: ducvm29 Date: Thu, 26 Sep 2024 15:52:52 +0700 Subject: [PATCH] Use subnet service for MFKE --- fptcloud/mfke/resource_mfke.go | 19 ++++++-------- fptcloud/subnet/subnetClient.go | 44 --------------------------------- 2 files changed, 8 insertions(+), 55 deletions(-) delete mode 100644 fptcloud/subnet/subnetClient.go diff --git a/fptcloud/mfke/resource_mfke.go b/fptcloud/mfke/resource_mfke.go index b7c636e..2973c99 100644 --- a/fptcloud/mfke/resource_mfke.go +++ b/fptcloud/mfke/resource_mfke.go @@ -42,7 +42,7 @@ var ( type resourceManagedKubernetesEngine struct { client *commons.Client mfkeClient *MfkeApiClient - subnetClient *fptcloud_subnet.SubnetClient + subnetClient fptcloud_subnet.SubnetService } func NewResourceManagedKubernetesEngine() resource.Resource { @@ -272,7 +272,7 @@ func (r *resourceManagedKubernetesEngine) Configure(ctx context.Context, request r.client = client r.mfkeClient = newMfkeApiClient(r.client) - r.subnetClient = fptcloud_subnet.NewSubnetClient(r.client) + r.subnetClient = fptcloud_subnet.NewSubnetService(r.client) } func (r *resourceManagedKubernetesEngine) topFields() map[string]schema.Attribute { topLevelAttributes := map[string]schema.Attribute{} @@ -705,21 +705,18 @@ func (r *resourceManagedKubernetesEngine) getOsVersion(ctx context.Context, vers return nil, &diag } -func getNetworkId(ctx context.Context, client *fptcloud_subnet.SubnetClient, vpcId string, networkName string) (string, error) { +func getNetworkId(ctx context.Context, client fptcloud_subnet.SubnetService, vpcId string, networkName string) (string, error) { tflog.Info(ctx, "Resolving network ID for VPC "+vpcId+", network "+networkName) - networks, err := client.ListNetworks(vpcId) + networks, err := client.FindSubnetByName(fptcloud_subnet.FindSubnetDTO{ + NetworkName: networkName, + VpcId: vpcId, + }) if err != nil { return "", err } - for _, network := range networks { - if network.Name == networkName { - return network.ID, nil - } - } - - return "", errors.New("couldn't find network with name " + networkName) + return networks.ID, nil } func getClusterName(name string) string { var indices []int diff --git a/fptcloud/subnet/subnetClient.go b/fptcloud/subnet/subnetClient.go deleted file mode 100644 index 25266e4..0000000 --- a/fptcloud/subnet/subnetClient.go +++ /dev/null @@ -1,44 +0,0 @@ -package fptcloud_subnet - -import ( - "encoding/json" - "terraform-provider-fptcloud/commons" -) - -type SubnetClient struct { - *commons.Client -} - -func NewSubnetClient(client *commons.Client) *SubnetClient { - return &SubnetClient{client} -} - -func (c *SubnetClient) ListNetworks(vpcId string) ([]SubnetData, error) { - url := commons.ApiPath.Subnet(vpcId) - res, err := c.SendGetRequest(url) - - if err != nil { - return nil, err - } - - var r subnetResponse - if err = json.Unmarshal(res, &r); err != nil { - return nil, err - } - - return r.Data, nil -} - -type SubnetData struct { - ID string `json:"id"` - Name string `json:"name"` - Description string `json:"description"` - DefaultGateway string `json:"defaultGateway"` - SubnetPrefixLength int `json:"subnetPrefixLength"` - NetworkID interface{} `json:"network_id"` - NetworkType string `json:"networkType"` -} - -type subnetResponse struct { - Data []SubnetData `json:"data"` -}