From 34cccb356b1a09f4897dfdd4a0ec46bfaaac74a2 Mon Sep 17 00:00:00 2001 From: ducvm29 Date: Mon, 9 Sep 2024 13:19:21 +0700 Subject: [PATCH] Refactor subnet resolution --- fptcloud/subnet/subnetClient.go | 44 +++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 fptcloud/subnet/subnetClient.go diff --git a/fptcloud/subnet/subnetClient.go b/fptcloud/subnet/subnetClient.go new file mode 100644 index 0000000..25266e4 --- /dev/null +++ b/fptcloud/subnet/subnetClient.go @@ -0,0 +1,44 @@ +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"` +}