Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test #21

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions commons/api_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ var ApiPath = struct {
DedicatedFKEUpgradeVersion func(vpcId string, clusterId string) string
DedicatedFKEManagement func(vpcId string, clusterId string) string

ManagedFKEList func(vpcId string, page int, pageSize int, infraType string) 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
ManagedFKEList func(vpcId string, page int, pageSize int, infraType string) 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
ManagedFKEHibernate func(vpcId, platform, clusterId string) string
ManagedFKEWakeup func(vpcId, platform, clusterId string) string
GetFKEOSVersion func(vpcId string, platform string) string
}{
SSH: "/v1/user/sshs",
Storage: func(vpcId string) string {
Expand Down Expand Up @@ -216,6 +218,18 @@ var ApiPath = struct {
vpcId, platform, clusterId,
)
},
ManagedFKEHibernate: func(vpcId, platform, clusterId string) string {
return fmt.Sprintf(
"/v1/xplat/fke/vpc/%s/m-fke/%s/hibernation-cluster/shoots/%s/hibernate",
vpcId, platform, clusterId,
)
},
ManagedFKEWakeup: func(vpcId, platform, clusterId string) string {
return fmt.Sprintf(
"/v1/xplat/fke/vpc/%s/m-fke/%s/hibernation-cluster/shoots/%s/wakeup",
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)
},
Expand Down
7 changes: 6 additions & 1 deletion fptcloud/dfke/resource_dfke_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,14 @@ func (r *resourceDedicatedKubernetesEngineState) Update(ctx context.Context, req
return
}

target := "stop"
if !state.IsRunning.ValueBool() {
target = "start"
}

management := dedicatedKubernetesEngineManagement{
ClusterId: state.Id.ValueString(),
MgmtAction: "stop",
MgmtAction: target,
DiskExtend: "0",
ExtendType: "",
Flavor: "",
Expand Down
70 changes: 70 additions & 0 deletions fptcloud/edge_gateway/datasource_edge_gateway_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package fptcloud_edge_gateway

import (
"context"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/stretchr/testify/assert"
"terraform-provider-fptcloud/commons"
"testing"
)

func TestReadEdgeGatewayDataSource(t *testing.T) {
vpcId := "123bbb12-12f3-1234-abcd-a12345678a0b"

mockResponse := `{
"data": [
{
"id": "123456ab-1234-5ab6-7891-011abc12345ab",
"vpc_id": "123bbb12-12f3-1234-abcd-a12345678a0b",
"name": "edge_gateway_1",
"description": null,
"edge_gateway_id": "56789ab-1234-12ab-34cd-12345668",
"vdc_group_id": null,
"vdc_group_name": null,
"created_at": "2020-01-01 00:00:00",
"ip_quotas": "1000",
"networks": []
},
{
"id": "123456ab-1234-5ab6-7891-011abc12346ab",
"vpc_id": "123bbb12-12f3-1234-abcd-a12345678a0b",
"name": "edge_gateway_2",
"description": null,
"edge_gateway_id": "56789ab-1234-12ab-34cd-12345669",
"vdc_group_id": null,
"vdc_group_name": null,
"created_at": "2020-01-01 00:00:00",
"ip_quotas": "1000",
"networks": []
}
]
}`

// Mock client and server setup
apiPath := commons.ApiPath.EdgeGatewayList(vpcId)
mockClient, server, _ := commons.NewClientForTesting(map[string]string{
apiPath: mockResponse,
})
defer server.Close()

state := edge_gateway{
VpcId: types.StringValue(vpcId),
}
d := datasourceEdgeGateway{
client: mockClient,
}

edgeGatewayList, err := d.internalRead(context.Background(), &state)

// Check if the edgeGatewayList is correct
assert.NoError(t, err)
assert.NotNil(t, edgeGatewayList)

assert.Equal(t, "123456ab-1234-5ab6-7891-011abc12345ab", (*edgeGatewayList)[0].Id)
assert.Equal(t, "edge_gateway_1", (*edgeGatewayList)[0].Name)
assert.Equal(t, "56789ab-1234-12ab-34cd-12345668", (*edgeGatewayList)[0].EdgeGatewayId)

assert.Equal(t, "123456ab-1234-5ab6-7891-011abc12346ab", (*edgeGatewayList)[1].Id)
assert.Equal(t, "edge_gateway_2", (*edgeGatewayList)[1].Name)
assert.Equal(t, "56789ab-1234-12ab-34cd-12345669", (*edgeGatewayList)[1].EdgeGatewayId)
}
40 changes: 25 additions & 15 deletions fptcloud/mfke/resource_mfke.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,21 +456,7 @@ func (r *resourceManagedKubernetesEngine) remapPools(item *managedKubernetesEngi
}

func (r *resourceManagedKubernetesEngine) checkForError(a []byte) *diag2.ErrorDiagnostic {
var re map[string]interface{}
err := json.Unmarshal(a, &re)
if err != nil {
res := diag2.NewErrorDiagnostic("Error unmarshalling response", err.Error())
return &res
}

if e, ok := re["error"]; ok {
if e == true {
res := diag2.NewErrorDiagnostic("Response contained an error field", "Response body was "+string(a))
return &res
}
}

return nil
return checkForError(a)
}

func (r *resourceManagedKubernetesEngine) diff(ctx context.Context, from *managedKubernetesEngine, to *managedKubernetesEngine) *diag2.ErrorDiagnostic {
Expand Down Expand Up @@ -787,6 +773,24 @@ func parseNumber(s string) int {
return f
}

func checkForError(a []byte) *diag2.ErrorDiagnostic {
var re map[string]interface{}
err := json.Unmarshal(a, &re)
if err != nil {
res := diag2.NewErrorDiagnostic("Error unmarshalling response", err.Error())
return &res
}

if e, ok := re["error"]; ok {
if e == true {
res := diag2.NewErrorDiagnostic("Response contained an error field", "Response body was "+string(a))
return &res
}
}

return nil
}

type managedKubernetesEngine struct {
Id types.String `tfsdk:"id"`
VpcId types.String `tfsdk:"vpc_id"`
Expand Down Expand Up @@ -946,6 +950,12 @@ type managedKubernetesEngineDataSpec struct {
} `json:"infrastructureConfig"`
Workers []*managedKubernetesEngineDataWorker `json:"workers"`
} `json:"provider"`

Hibernation *managedKubernetesEngineDataSpecHibernate `json:"hibernation"`
}

type managedKubernetesEngineDataSpecHibernate struct {
Enabled *bool `json:"enabled"`
}

type managedKubernetesEngineDataWorker struct {
Expand Down
Loading