Skip to content

Commit

Permalink
Merge pull request #254 from HewlettPackard/add-ds-storageType
Browse files Browse the repository at this point in the history
Add DS storage_type for instance
  • Loading branch information
reubenur-rahman authored Sep 12, 2024
2 parents aa43195 + df751d5 commit cd20a56
Show file tree
Hide file tree
Showing 18 changed files with 209 additions and 32 deletions.
5 changes: 5 additions & 0 deletions acc-dev-testcases/resources/instance_clone.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ acc:
network {
id = 156
}
volume {
name = "root_vol"
datastore_id = "auto"
size = 6
}
5 changes: 5 additions & 0 deletions acc-external-testcases/resources/instance_clone.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ acc:
network {
id = 119
}
volume {
name = "root_vol"
datastore_id = "auto"
size = 6
}
5 changes: 5 additions & 0 deletions acc-prod_testcases/resources/instance_clone.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ acc:
network {
id = 84
interface_id = 4
}
volume {
name = "root_vol"
datastore_id = "auto"
size = 6
}
7 changes: 7 additions & 0 deletions acc-testcases/data-sources/instance_disk_type.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
acc:
- config: |
name = "thin"
cloud_id = 1
layout_id = 376
validations:
tf.id: 133
5 changes: 5 additions & 0 deletions acc-testcases/resources/instance_clone.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ acc:
network {
id = 97
interface_id = 4
}
volume {
name = "root_vol"
datastore_id = "auto"
size = 6
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# (C) Copyright 2024 Hewlett Packard Enterprise Development LP

data "hpegl_vmaas_instance_disk_type" "vmaas_cloud_vmware_thin_lazy" {
name = "thick (lazy zero)"
cloud_id = data.hpegl_vmaas_cloud.cloud.id
layout_id = data.hpegl_vmaas_layout.vmware.id
}
8 changes: 4 additions & 4 deletions examples/data-sources/hpegl_vmaas_layout/data-source.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# (C) Copyright 2021 Hewlett Packard Enterprise Development LP
# (C) Copyright 2021-2024 Hewlett Packard Enterprise Development LP

data "hpegl_vmaas_layout" "vmware_centos" {
name = "VMware VM with vanilla CentOS"
instance_type_code = "glhc-vanilla-centos"
data "hpegl_vmaas_layout" "vmware" {
name = "Vmware VM"
instance_type_code = "vmware"
}
4 changes: 3 additions & 1 deletion examples/resources/hpegl_vmaas_instance/all_options.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# (C) Copyright 2021 Hewlett Packard Enterprise Development LP
# (C) Copyright 2021-2024 Hewlett Packard Enterprise Development LP

# create instance with all possible options
resource "hpegl_vmaas_instance" "tf_instance" {
Expand All @@ -19,12 +19,14 @@ resource "hpegl_vmaas_instance" "tf_instance" {
name = "root_vol"
size = 5
datastore_id = data.hpegl_vmaas_datastore.c_3par.id
storage_type = data.hpegl_vmaas_instance_disk_type.vmaas_cloud_vmware_thin_lazy.id
}

volume {
name = "local_vol"
size = 5
datastore_id = data.hpegl_vmaas_datastore.c_3par.id
storage_type = data.hpegl_vmaas_instance_disk_type.vmware_thin.id
}

labels = ["test_label"]
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/HewlettPackard/hpegl-vmaas-terraform-resources
go 1.21

require (
github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.10
github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.11-0.20240911094737-0affabbd1c72
github.com/golang/mock v1.6.0
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/terraform-plugin-docs v0.9.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk=
dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.10 h1:RLRtdCjl12eunpAxSNrlUJTGFALbDR0lmZuW/ICbiOQ=
github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.10/go.mod h1:UzXizYR6sUVsvDRMcLC2E4eabJsPH/3nM8rlKL2BLSc=
github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.11-0.20240910105957-f3f6f72246f7 h1:Aw+QRmQD7A1YPydzX4R1+Y+aQdtMVMfBQoWXkBYOgM0=
github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.11-0.20240910105957-f3f6f72246f7/go.mod h1:UzXizYR6sUVsvDRMcLC2E4eabJsPH/3nM8rlKL2BLSc=
github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.11-0.20240911094737-0affabbd1c72 h1:6JonchRsW6qhkkO5TVMzRz8SyhV2Ln3+R5VjNjbchaU=
github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.11-0.20240911094737-0affabbd1c72/go.mod h1:UzXizYR6sUVsvDRMcLC2E4eabJsPH/3nM8rlKL2BLSc=
github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
Expand Down
29 changes: 29 additions & 0 deletions internal/acceptance_test/data_source_instance_storage_type_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// (C) Copyright 2024 Hewlett Packard Enterprise Development LP

package acceptancetest

import (
"testing"

api_client "github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk/pkg/client"
"github.com/HewlettPackard/hpegl-vmaas-terraform-resources/pkg/atf"
)

func TestAccDataSourceInstanceStorageType(t *testing.T) {
acc := &atf.Acc{
PreCheck: testAccPreCheck,
Providers: testAccProviders,
ResourceName: "hpegl_vmaas_instance_disk_type",
GetAPI: func(attr map[string]string) (interface{}, error) {
cl, cfg := getAPIClient()
iClient := api_client.InstancesAPIService{
Client: cl,
Cfg: cfg,
}

return iClient.GetStorageVolTypeID(getAccContext(), attr["cloud_id"], attr["layout_id"])
},
}

acc.RunDataSourceTests(t)
}
4 changes: 3 additions & 1 deletion internal/cmp/client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// (C) Copyright 2021-2022 Hewlett Packard Enterprise Development LP
// (C) Copyright 2021-2024 Hewlett Packard Enterprise Development LP

package cmp

Expand Down Expand Up @@ -47,6 +47,7 @@ type Client struct {
DSLBMonitor DataSource
DSPoolMemeberGroup DataSource
DSDhcpServer DataSource
InstanceStorageType DataSource
}

// NewClient returns configured client
Expand Down Expand Up @@ -113,5 +114,6 @@ func NewClient(client *apiClient.APIClient, cfg apiClient.Configuration) *Client
NetworkProxy: newNetworkProxy(&apiClient.NetworksAPIService{Client: client, Cfg: cfg}),
TransportZone: newTransportZone(&apiClient.RouterAPIService{Client: client, Cfg: cfg}),
EdgeCluster: newEdgeCluster(&apiClient.RouterAPIService{Client: client, Cfg: cfg}),
InstanceStorageType: newInstanceStorageType(&apiClient.InstancesAPIService{Client: client, Cfg: cfg}),
}
}
30 changes: 8 additions & 22 deletions internal/cmp/instance_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ func instanceGetVolume(volumes []map[string]interface{}) []models.CreateInstance
Name: volumes[i]["name"].(string),
Size: volumes[i]["size"].(int),
DatastoreID: volumes[i]["datastore_id"],
StorageType: volumes[i]["storage_type"].(int),
})
}
volumesModel[0].RootVolume = true
Expand All @@ -234,6 +235,7 @@ func instanceResizeVolume(volumes []map[string]interface{}) []models.ResizeInsta
Name: volumes[i]["name"].(string),
Size: volumes[i]["size"].(int),
DatastoreID: volumes[i]["datastore_id"],
StorageType: volumes[i]["storage_type"].(int),
})
}

Expand Down Expand Up @@ -278,6 +280,7 @@ func instanceSetVolume(volumes []models.GetInstanceResponseInstanceVolumes) []mo
Size: volumes[i].Size,
DatastoreID: volumes[i].DatastoreID.(string),
Root: volumes[i].RootVolume,
StorageType: volumes[i].StorageType,
})
}

Expand Down Expand Up @@ -394,33 +397,16 @@ func instanceCloneCompareVolume(
newVolumes := make([]models.CreateInstanceBodyVolumes, 0, len(vSchemas))

// convert schema volume to model
for i := range newVolumes {
for _, vol := range vSchemas {
newVolumes = append(newVolumes, models.CreateInstanceBodyVolumes{
ID: -1,
Size: vSchemas[i]["size"].(int),
DatastoreID: vSchemas[i]["datastore_id"],
Size: vol["size"].(int),
Name: vol["name"].(string),
DatastoreID: vol["datastore_id"],
StorageType: vol["storage_type"].(int),
})
}

// check parent instance have same volume name, if so use same id
for _, VModel := range vModels {
volumeExist := false
for i, v := range newVolumes {
if VModel.Name == v.Name {
newVolumes[i].ID = VModel.ID
volumeExist = true
}
}
// if parent instance volume not exist in schema add it in request
if !volumeExist {
newVolumes = append(newVolumes, models.CreateInstanceBodyVolumes{
ID: VModel.ID,
Size: VModel.Size,
DatastoreID: VModel.DatastoreID,
Name: VModel.Name,
})
}
}
newVolumes[0].RootVolume = true

return newVolumes
Expand Down
54 changes: 54 additions & 0 deletions internal/cmp/instance_storage_type_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// (C) Copyright 2024 Hewlett Packard Enterprise Development LP

package cmp

import (
"context"
"fmt"
"log"
"strings"

"github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk/pkg/client"
"github.com/HewlettPackard/hpegl-vmaas-terraform-resources/internal/utils"
"github.com/tshihad/tftags"
)

type instanceStorageType struct {
iClient *client.InstancesAPIService
}

func newInstanceStorageType(instanceClient *client.InstancesAPIService) *instanceStorageType {
return &instanceStorageType{iClient: instanceClient}
}

func (i *instanceStorageType) Read(ctx context.Context, d *utils.Data, meta interface{}) error {
setMeta(meta, i.iClient.Client)
log.Printf("[DEBUG] Get Instance Storage Volume Type")
name := d.GetString("name")
name = strings.ToLower(name)
name = strings.TrimSpace(name)
cloudID := d.GetString("cloud_id")
layoutID := d.GetString("layout_id")

// Pre check
if err := d.Error(); err != nil {
return err
}

storageType, err := i.iClient.GetStorageVolTypeID(ctx, cloudID, layoutID)
if err != nil {
return err
}

for _, n := range storageType.Plans[0].StorageTypes {
volType := strings.ToLower(n.Name)
volType = strings.TrimSpace(volType)
if volType == name {
log.Print("[DEBUG] Storage type ID ", n.ID)

return tftags.Set(d, n)
}
}

return fmt.Errorf(errExactMatch, "Instance Storage Type")
}
3 changes: 2 additions & 1 deletion internal/resources/constants.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// (C) Copyright 2021-2022 Hewlett Packard Enterprise Development LP
// (C) Copyright 2021-2024 Hewlett Packard Enterprise Development LP

package resources

Expand Down Expand Up @@ -31,6 +31,7 @@ const (
DSLBVirtualServer = "hpegl_vmaas_load_balancer_virtual_server"
DSLBVirtualServerSslCert = "hpegl_vmaas_load_balancer_virtual_server_ssl_cert"
DSDhcpServer = "hpegl_vmaas_dhcp_server"
DSInstanceStorageType = "hpegl_vmaas_instance_disk_type"

// resource key
ResInstance = "hpegl_vmaas_instance"
Expand Down
58 changes: 58 additions & 0 deletions internal/resources/data_source_instance_storage_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// (C) Copyright 2024 Hewlett Packard Enterprise Development LP

package resources

import (
"context"

"github.com/HewlettPackard/hpegl-vmaas-terraform-resources/internal/utils"
"github.com/HewlettPackard/hpegl-vmaas-terraform-resources/pkg/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func ReadInstanceStorageType() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: f(generalNamedesc, "disk type", "disk type"),
},
"cloud_id": {
Type: schema.TypeString,
Required: true,
Description: f(generalDDesc, "cloud"),
},
"layout_id": {
Type: schema.TypeString,
Required: true,
Description: f(generalDDesc, "layout"),
},
},
ReadContext: readInstanceStorageTypeContext,
Description: `The ` + DSInstanceStorageType + ` data source can be used to discover the ID of a disk type.
This can then be used with resources or data sources that require a ` + DSInstanceStorageType + `,
such as the ` + ResInstance + ` resource.`,
SchemaVersion: 0,
StateUpgraders: nil,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}

func readInstanceStorageTypeContext(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, err := client.GetClientFromMetaMap(meta)
if err != nil {
return diag.FromErr(err)
}

data := utils.NewData(d)
err = c.CmpClient.InstanceStorageType.Read(ctx, data, meta)
if err != nil {
return diag.FromErr(err)
}

return nil
}
10 changes: 8 additions & 2 deletions internal/resources/resource_instances_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ func getInstanceDefaultSchema(isClone bool) *schema.Resource {
},
"volume": {
Type: schema.TypeList,
Optional: isClone,
Required: !isClone,
Required: true,
MinItems: 1,
Description: `A list of volumes to be created inside a provisioned instance.
It can have a root volume and other secondary volumes.`,
Expand All @@ -143,6 +142,13 @@ func getInstanceDefaultSchema(isClone bool) *schema.Resource {
data source. Use the value 'auto' so that the datastore is automatically selected.`,
DiffSuppressFunc: utils.SkipField(),
},
"storage_type": {
Type: schema.TypeInt,
Optional: true,
Description: `Storage type ID can be obtained from hpegl_vmaas_instance_disk_type
data source.`,
DiffSuppressFunc: utils.SkipField(),
},
"id": {
Computed: true,
Type: schema.TypeInt,
Expand Down
1 change: 1 addition & 0 deletions pkg/resources/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (r Registration) SupportedDataSources() map[string]*schema.Resource {
resources.DSPoolMemeberGroup: resources.LBPoolMemeberGroupData(),
resources.DSLBVirtualServerSslCert: resources.LBVirtualServerSslCertData(),
resources.DSDhcpServer: resources.DhcpServerData(),
resources.DSInstanceStorageType: resources.ReadInstanceStorageType(),
}
}

Expand Down

0 comments on commit cd20a56

Please sign in to comment.