Skip to content

Commit

Permalink
Add managed FKE implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ducvm29 committed Aug 19, 2024
1 parent 43a26cb commit 0777307
Show file tree
Hide file tree
Showing 6 changed files with 746 additions and 19 deletions.
31 changes: 31 additions & 0 deletions commons/api_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ var ApiPath = struct {
StoragePolicy func(vpcId string) string
Flavor func(vpcId string) string
Image func(vpcId string) string
Subnet func(vpcId string) string
ManagedFKEList func(vpcId string, page int, pageSize int) string
ManagedFKEGet func(vpcId string, platform string, clusterId string) string
ManagedFKEDelete func(vpcId string, platform string, clusterName string) string
ManagedFKECreate func(vpcId string, platform string) string
GetFKEOSVersion func(vpcId string, platform string) string
}{
SSH: "/v1/user/sshs",
Storage: func(vpcId string) string {
Expand All @@ -26,4 +32,29 @@ var ApiPath = struct {
Image: func(vpcId string) string {
return fmt.Sprintf("/v1/terraform/vpc/%s/images", vpcId)
},
Subnet: func(vpcId string) string { return fmt.Sprintf("/v1/vmware/vpc/%s/network/subnets", vpcId) },
ManagedFKEList: func(vpcId string, page int, pageSize int) string {
return fmt.Sprintf("/v1/xplat/fke/vpc/%s/m-fke/vmw/get-shoot-cluster/shoots?page=%d&page_size=%d", vpcId, page, pageSize)
},
ManagedFKEDelete: func(vpcId string, platform string, clusterName string) string {
return fmt.Sprintf(
"/v1/xplat/fke/vpc/%s/m-fke/%s/delete-shoot-cluster/shoots/%s",
vpcId, platform, clusterName,
)
},
ManagedFKECreate: func(vpcId string, platform string) string {
return fmt.Sprintf(
"/v1/xplat/fke/vpc/%s/m-fke/%s/create-cluster",
vpcId, platform,
)
},
ManagedFKEGet: func(vpcId string, platform string, clusterId string) string {
return fmt.Sprintf(
"/v1/xplat/fke/vpc/%s/m-fke/%s/get-shoot-specific/shoots/%s",
vpcId, platform, clusterId,
)
},
GetFKEOSVersion: func(vpcId string, platform string) string {
return fmt.Sprintf("v1/xplat/fke/vpc/%s/m-fke/%s/get_k8s_versions", vpcId, platform)
},
}
12 changes: 8 additions & 4 deletions fptcloud/dfke/dfke_service.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package fptcloud_dfke

import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/hashicorp/terraform-plugin-log/tflog"
"strings"
"terraform-provider-fptcloud/commons"
)
Expand Down Expand Up @@ -42,8 +44,9 @@ type edgeResponse struct {
EdgeGateway EdgeGateway `json:"edgeGateway"`
}

func (a *dfkeApiClient) FindEdgeById(vpcId string, id string) (*EdgeGateway, error) {
path := fmt.Sprintf("internal/vpc/%s/find_edge_by_id/%s/false", vpcId, id)
func (a *dfkeApiClient) FindEdgeById(ctx context.Context, vpcId string, id string) (*EdgeGateway, error) {
tflog.Info(ctx, "Resolving edge by ID "+id)
path := fmt.Sprintf("/v1/kubernetes/vpc/%s/find_edge_by_id/%s/false", vpcId, id)
r, err := a.internalFindEdge(path)
if err != nil {
return nil, err
Expand All @@ -52,11 +55,12 @@ func (a *dfkeApiClient) FindEdgeById(vpcId string, id string) (*EdgeGateway, err
return r, nil
}

func (a *dfkeApiClient) FindEdgeByEdgeGatewayId(vpcId string, edgeId string) (*EdgeGateway, error) {
func (a *dfkeApiClient) FindEdgeByEdgeGatewayId(ctx context.Context, vpcId string, edgeId string) (*EdgeGateway, error) {
if !strings.HasPrefix(edgeId, "urn:vcloud:gateway") {
return nil, errors.New("edge gateway id must be prefixed with \"urn:vcloud:gateway\"")
}
path := fmt.Sprintf("internal/vpc/%s/find_edge_by_id/%s/true", vpcId, edgeId)
tflog.Info(ctx, "Resolving edge by gateway ID "+edgeId)
path := fmt.Sprintf("/v1/kubernetes/vpc/%s/find_edge_by_id/%s/true", vpcId, edgeId)
r, err := a.internalFindEdge(path)
if err != nil {
return nil, err
Expand Down
51 changes: 41 additions & 10 deletions fptcloud/dfke/resource_dfke.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ func (r *resourceDedicatedKubernetesEngine) Create(ctx context.Context, request
f.EnableCustomScript = false
f.PublicKey = ""
f.UpstreamDNS = ""
f.RegionId = "saigon-vn"

client := r.client
a, err := client.SendPostRequest(fmt.Sprintf("xplat/fke/vpc/%s/kubernetes", state.vpcId()), f)
a, err := client.SendPostRequest(fmt.Sprintf("/v1/xplat/fke/vpc/%s/kubernetes", state.vpcId()), f)

if err != nil {
response.Diagnostics.Append(diag2.NewErrorDiagnostic("Error calling API", err.Error()))
Expand Down Expand Up @@ -109,8 +108,25 @@ func (r *resourceDedicatedKubernetesEngine) Read(ctx context.Context, request re
}

func (r *resourceDedicatedKubernetesEngine) Update(ctx context.Context, request resource.UpdateRequest, response *resource.UpdateResponse) {
//TODO implement me
panic("implement me")
var state dedicatedKubernetesEngine
diags := request.State.Get(ctx, &state)

response.Diagnostics.Append(diags...)
if response.Diagnostics.HasError() {
return
}

err := r.internalRead(ctx, state.Id.ValueString(), &state)
if err != nil {
response.Diagnostics.Append(diag2.NewErrorDiagnostic("Error calling API", err.Error()))
return
}

diags = response.State.Set(ctx, &state)
response.Diagnostics.Append(diags...)
if response.Diagnostics.HasError() {
return
}
}

func (r *resourceDedicatedKubernetesEngine) Delete(ctx context.Context, request resource.DeleteRequest, response *resource.DeleteResponse) {
Expand All @@ -122,16 +138,18 @@ func (r *resourceDedicatedKubernetesEngine) Delete(ctx context.Context, request
return
}

_, err := r.client.SendDeleteRequest(fmt.Sprintf("xplat/fke/vpc/%s/cluster/%s/delete", state.vpcId(), state.Id))
_, err := r.client.SendDeleteRequest(fmt.Sprintf("/v1/xplat/fke/vpc/%s/cluster/%s/delete", state.vpcId(), state.Id))
if err != nil {
response.Diagnostics.Append(diag2.NewErrorDiagnostic("Error calling API", err.Error()))
return
}
}

func (r *resourceDedicatedKubernetesEngine) ImportState(ctx context.Context, request resource.ImportStateRequest, response *resource.ImportStateResponse) {
tflog.Info(ctx, "Importing cluster ID "+request.ID)
tflog.Info(ctx, "Importing DFKE cluster ID "+request.ID)

var state dedicatedKubernetesEngine
state.VpcId = types.StringValue("188af427-269b-418a-90bb-0cb27afc6c1e")

state.Id = types.StringValue(request.ID)
err := r.internalRead(ctx, request.ID, &state)
Expand All @@ -145,6 +163,10 @@ func (r *resourceDedicatedKubernetesEngine) ImportState(ctx context.Context, req
if response.Diagnostics.HasError() {
return
}

// lack of ability to import without VPC ID
//response.Diagnostics.Append(diag2.NewErrorDiagnostic("Unimplemented", "Importing DFKE clusters isn't currently supported"))
//return
}

func NewResourceDedicatedKubernetesEngine() resource.Resource {
Expand Down Expand Up @@ -259,6 +281,14 @@ func (r *resourceDedicatedKubernetesEngine) Schema(ctx context.Context, request
Required: true,
PlanModifiers: forceNewPlanModifiersString,
},
"vpc_id": schema.StringAttribute{
Required: true,
PlanModifiers: forceNewPlanModifiersString,
},
"region_id": schema.StringAttribute{
Required: true,
PlanModifiers: forceNewPlanModifiersString,
},
},
}
}
Expand Down Expand Up @@ -296,7 +326,7 @@ func (r *resourceDedicatedKubernetesEngine) internalRead(ctx context.Context, cl
vpcId := state.VpcId.ValueString()
tflog.Info(ctx, "Reading state of cluster ID "+clusterId+", VPC ID "+vpcId)

a, err := r.client.SendGetRequest(fmt.Sprintf("xplat/fke/vpc/%s/cluster/%s?page=1&page_size=25", vpcId, clusterId))
a, err := r.client.SendGetRequest(fmt.Sprintf("/v1/xplat/fke/vpc/%s/cluster/%s?page=1&page_size=25", vpcId, clusterId))

if err != nil {
return err
Expand All @@ -317,7 +347,7 @@ func (r *resourceDedicatedKubernetesEngine) internalRead(ctx context.Context, cl
}

// resolve edge ID
edge, err := r.dfkeClient.FindEdgeByEdgeGatewayId(vpcId, data.EdgeID)
edge, err := r.dfkeClient.FindEdgeByEdgeGatewayId(ctx, vpcId, data.EdgeID)
if err != nil {
return err
}
Expand Down Expand Up @@ -393,6 +423,7 @@ func (r *resourceDedicatedKubernetesEngine) remap(from *dedicatedKubernetesEngin
to.NodeDNS = from.NodeDNS.ValueString()
to.IPPublicFirewall = from.IPPublicFirewall.ValueString()
to.IPPrivateFirewall = from.IPPrivateFirewall.ValueString()
to.RegionId = from.RegionId.ValueString()
}

func (e *dedicatedKubernetesEngine) vpcId() string {
Expand Down Expand Up @@ -431,6 +462,7 @@ type dedicatedKubernetesEngine struct {
IPPublicFirewall types.String `tfsdk:"ip_public_firewall" json:"ip_public_firewall"`
IPPrivateFirewall types.String `tfsdk:"ip_private_firewall" json:"ip_private_firewall"`
VpcId types.String `tfsdk:"vpc_id" json:"vpc_id"`
RegionId types.String `tfsdk:"region_id"`
}

type dedicatedKubernetesEngineJson struct {
Expand Down Expand Up @@ -470,8 +502,7 @@ type dedicatedKubernetesEngineJson struct {
EnableCustomScript bool `json:"enable_custom_script"`
PublicKey string `json:"public_key"`
UpstreamDNS string `json:"upstream_dns"`

RegionId string `json:"region_id"`
RegionId string `json:"region_id"`
}

type dedicatedKubernetesEngineData struct {
Expand Down
Loading

0 comments on commit 0777307

Please sign in to comment.