|
1 | 1 | package model |
2 | 2 |
|
3 | | -import "github.com/hashicorp/terraform-plugin-framework/types" |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 7 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 8 | + console "github.com/pluralsh/console-client-go" |
| 9 | +) |
4 | 10 |
|
5 | 11 | // Cluster describes the cluster resource and data source model. |
6 | 12 | type Cluster struct { |
7 | 13 | Id types.String `tfsdk:"id"` |
8 | 14 | InseredAt types.String `tfsdk:"inserted_at"` |
9 | 15 | Name types.String `tfsdk:"name"` |
10 | 16 | Handle types.String `tfsdk:"handle"` |
| 17 | + Version types.String `tfsdk:"version"` |
| 18 | + ProviderId types.String `tfsdk:"provider_id"` |
11 | 19 | Cloud types.String `tfsdk:"cloud"` |
12 | 20 | CloudSettings ClusterCloudSettings `tfsdk:"cloud_settings"` |
13 | 21 | Protect types.Bool `tfsdk:"protect"` |
@@ -66,3 +74,98 @@ type KubeconfigExec struct { |
66 | 74 | Env types.String `tfsdk:"env"` |
67 | 75 | APIVersion types.String `tfsdk:"api_version"` |
68 | 76 | } |
| 77 | + |
| 78 | +func (c *Cluster) CloudSettingsAttributes() *console.CloudSettingsAttributes { |
| 79 | + if IsCloud(c.Cloud.ValueString(), CloudAWS) { |
| 80 | + return &console.CloudSettingsAttributes{ |
| 81 | + Aws: &console.AwsCloudAttributes{ |
| 82 | + Region: c.CloudSettings.AWS.Region.ValueStringPointer(), |
| 83 | + }, |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + if IsCloud(c.Cloud.ValueString(), CloudAzure) { |
| 88 | + return &console.CloudSettingsAttributes{ |
| 89 | + Azure: &console.AzureCloudAttributes{ |
| 90 | + Location: c.CloudSettings.Azure.Location.ValueStringPointer(), |
| 91 | + SubscriptionID: c.CloudSettings.Azure.SubscriptionId.ValueStringPointer(), |
| 92 | + ResourceGroup: c.CloudSettings.Azure.ResourceGroup.ValueStringPointer(), |
| 93 | + Network: c.CloudSettings.Azure.Network.ValueStringPointer(), |
| 94 | + }, |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if IsCloud(c.Cloud.ValueString(), CloudGCP) { |
| 99 | + return &console.CloudSettingsAttributes{ |
| 100 | + Gcp: &console.GcpCloudAttributes{ |
| 101 | + Project: c.CloudSettings.GCP.Project.ValueStringPointer(), |
| 102 | + Network: c.CloudSettings.GCP.Network.ValueStringPointer(), |
| 103 | + Region: c.CloudSettings.GCP.Region.ValueStringPointer(), |
| 104 | + }, |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return nil |
| 109 | +} |
| 110 | + |
| 111 | +func (c *Cluster) TagsAttribute() (result []*console.TagAttributes) { |
| 112 | + elements := make(map[string]types.String, len(c.Tags.Elements())) |
| 113 | + _ = c.Tags.ElementsAs(context.TODO(), &elements, false) // TODO: Context and skipped diagnostics. |
| 114 | + for k, v := range elements { |
| 115 | + result = append(result, &console.TagAttributes{Name: k, Value: v.ValueString()}) |
| 116 | + } |
| 117 | + |
| 118 | + return |
| 119 | +} |
| 120 | + |
| 121 | +func (c *Cluster) CreateAttributes() console.ClusterAttributes { |
| 122 | + return console.ClusterAttributes{ |
| 123 | + Name: c.Name.ValueString(), |
| 124 | + Handle: c.Handle.ValueStringPointer(), |
| 125 | + Version: c.Version.ValueStringPointer(), |
| 126 | + ProviderID: c.ProviderId.ValueStringPointer(), |
| 127 | + CloudSettings: c.CloudSettingsAttributes(), |
| 128 | + Tags: c.TagsAttribute(), |
| 129 | + Protect: c.Protect.ValueBoolPointer(), |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func (c *Cluster) UpdateAttributes() console.ClusterUpdateAttributes { |
| 134 | + return console.ClusterUpdateAttributes{ |
| 135 | + Handle: c.Handle.ValueStringPointer(), |
| 136 | + Version: c.Version.ValueStringPointer(), |
| 137 | + Protect: c.Protect.ValueBoolPointer(), |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +func (c *Cluster) TagsFrom(tags []*console.ClusterTags) { |
| 142 | + elements := map[string]attr.Value{} |
| 143 | + for _, v := range tags { |
| 144 | + elements[v.Name] = types.StringValue(v.Value) |
| 145 | + } |
| 146 | + |
| 147 | + tagsValue, _ := types.MapValue(types.StringType, elements) // TODO: Skipped diagnostics. |
| 148 | + c.Tags = tagsValue |
| 149 | +} |
| 150 | + |
| 151 | +func (c *Cluster) From(cl *console.ClusterFragment) { |
| 152 | + c.Id = types.StringValue(cl.ID) |
| 153 | + c.InseredAt = types.StringPointerValue(cl.InsertedAt) |
| 154 | + c.Name = types.StringValue(cl.Name) |
| 155 | + c.Handle = types.StringPointerValue(cl.Handle) |
| 156 | + c.Version = types.StringPointerValue(cl.Version) |
| 157 | + c.ProviderId = types.StringValue(cl.Provider.ID) |
| 158 | + c.Protect = types.BoolPointerValue(cl.Protect) |
| 159 | + c.TagsFrom(cl.Tags) |
| 160 | +} |
| 161 | + |
| 162 | +func (c *Cluster) FromCreate(cc *console.CreateCluster) { |
| 163 | + c.Id = types.StringValue(cc.CreateCluster.ID) |
| 164 | + c.InseredAt = types.StringPointerValue(cc.CreateCluster.InsertedAt) |
| 165 | + c.Name = types.StringValue(cc.CreateCluster.Name) |
| 166 | + c.Handle = types.StringPointerValue(cc.CreateCluster.Handle) |
| 167 | + c.Version = types.StringPointerValue(cc.CreateCluster.Version) |
| 168 | + c.ProviderId = types.StringValue(cc.CreateCluster.Provider.ID) |
| 169 | + c.Protect = types.BoolPointerValue(cc.CreateCluster.Protect) |
| 170 | + c.TagsFrom(cc.CreateCluster.Tags) |
| 171 | +} |
0 commit comments