generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 41
/
kubernetes_cloud_cost.go
99 lines (79 loc) · 3 KB
/
kubernetes_cloud_cost.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package connector
import (
"context"
"github.com/harness/harness-go-sdk/harness/nextgen"
"github.com/harness/terraform-provider-harness/helpers"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func ResourceConnectorKubernetesCloudCost() *schema.Resource {
resource := &schema.Resource{
Description: "Resource for creating a Kubernetes Cloud Cost connector.",
ReadContext: resourceConnectorKubernetesCloudCostRead,
CreateContext: resourceConnectorKubernetesCloudCostCreateOrUpdate,
UpdateContext: resourceConnectorKubernetesCloudCostCreateOrUpdate,
DeleteContext: resourceConnectorDelete,
Importer: helpers.MultiLevelResourceImporter,
Schema: map[string]*schema.Schema{
"connector_ref": {
Description: "Reference of the Connector." + helpers.Descriptions.ConnectorRefText.String(),
Type: schema.TypeString,
Required: true,
},
"features_enabled": {
Description: "Indicates which feature to enable among Billing, Optimization, and Visibility.",
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{"BILLING", "OPTIMIZATION", "VISIBILITY"}, false),
},
},
},
}
helpers.SetMultiLevelResourceSchema(resource.Schema)
return resource
}
func resourceConnectorKubernetesCloudCostRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn, err := resourceConnectorReadBase(ctx, d, meta, nextgen.ConnectorTypes.CEK8sCluster)
if err != nil {
return err
}
if conn == nil {
return nil
}
if err := readConnectorKubernetesCloudCost(d, conn); err != nil {
return diag.FromErr(err)
}
return nil
}
func resourceConnectorKubernetesCloudCostCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := buildConnectorKubernetesCloudCost(d)
newConn, err := resourceConnectorCreateOrUpdateBase(ctx, d, meta, conn)
if err != nil {
return err
}
if err := readConnectorKubernetesCloudCost(d, newConn); err != nil {
return diag.FromErr(err)
}
return nil
}
func buildConnectorKubernetesCloudCost(d *schema.ResourceData) *nextgen.ConnectorInfo {
connector := &nextgen.ConnectorInfo{
Type_: nextgen.ConnectorTypes.CEK8sCluster,
K8sClusterCloudCost: &nextgen.CeKubernetesClusterConfigDto{},
}
if attr, ok := d.GetOk("features_enabled"); ok {
connector.K8sClusterCloudCost.FeaturesEnabled = helpers.ExpandField(attr.(*schema.Set).List())
}
if attr, ok := d.GetOk("connector_ref"); ok {
connector.K8sClusterCloudCost.ConnectorRef = attr.(string)
}
return connector
}
func readConnectorKubernetesCloudCost(d *schema.ResourceData, connector *nextgen.ConnectorInfo) error {
d.Set("connector_ref", connector.K8sClusterCloudCost.ConnectorRef)
d.Set("features_enabled", connector.K8sClusterCloudCost.FeaturesEnabled)
return nil
}