Skip to content

Commit

Permalink
[Fix] Modify CCE cluster resources to match changes in gopher (#2751)
Browse files Browse the repository at this point in the history
[Fix] Modify CCE cluster resources to match changes in gopher

Summary of the Pull Request
CCE V3 Cluster functions changed in Gophercloud (PR #761) require modifications in TF provider.
PR Checklist

 Tests added/passed.
 Release notes added.

Acceptance Steps Performed
=== RUN   TestAccCCEClusterV3DataSource_basic
=== PAUSE TestAccCCEClusterV3DataSource_basic
=== RUN   TestAccCCEClusterV3_basic
=== PAUSE TestAccCCEClusterV3_basic
=== RUN   TestAccCCEClusterV3_turbo_basic
=== PAUSE TestAccCCEClusterV3_turbo_basic
=== RUN   TestAccCCEClusterV3_importBasic
=== PAUSE TestAccCCEClusterV3_importBasic
=== RUN   TestAccCCEClusterV3_invalidNetwork
=== PAUSE TestAccCCEClusterV3_invalidNetwork
=== RUN   TestAccCCEClusterV3_proxyAuth
=== PAUSE TestAccCCEClusterV3_proxyAuth
=== RUN   TestAccCCEClusterV3_timeout
=== PAUSE TestAccCCEClusterV3_timeout
=== RUN   TestAccCCEClusterV3NoAddons
=== PAUSE TestAccCCEClusterV3NoAddons
=== RUN   TestAccCCEClusterV3NoUserClusterDataSet
=== PAUSE TestAccCCEClusterV3NoUserClusterDataSet
=== RUN   TestAccCCEClusterV3_withVersionDiff
=== PAUSE TestAccCCEClusterV3_withVersionDiff
=== CONT  TestAccCCEClusterV3DataSource_basic
=== CONT  TestAccCCEClusterV3_proxyAuth
=== CONT  TestAccCCEClusterV3_importBasic
=== CONT  TestAccCCEClusterV3_invalidNetwork
=== CONT  TestAccCCEClusterV3NoUserClusterDataSet
=== CONT  TestAccCCEClusterV3NoAddons
=== CONT  TestAccCCEClusterV3_withVersionDiff
=== CONT  TestAccCCEClusterV3_timeout
=== CONT  TestAccCCEClusterV3_turbo_basic
=== CONT  TestAccCCEClusterV3_basic
--- PASS: TestAccCCEClusterV3_invalidNetwork (23.25s)
--- PASS: TestAccCCEClusterV3_proxyAuth (465.83s)
--- PASS: TestAccCCEClusterV3_importBasic (480.17s)
--- PASS: TestAccCCEClusterV3NoUserClusterDataSet (500.49s)
--- PASS: TestAccCCEClusterV3DataSource_basic (506.72s)
--- PASS: TestAccCCEClusterV3NoAddons (567.46s)
--- PASS: TestAccCCEClusterV3_withVersionDiff (886.22s)
--- PASS: TestAccCCEClusterV3_turbo_basic (915.85s)
--- PASS: TestAccCCEClusterV3_timeout (953.18s)
--- PASS: TestAccCCEClusterV3_basic (971.63s)
=== RUN   TestAccCCEKubeConfigV3DataSource_basic
=== PAUSE TestAccCCEKubeConfigV3DataSource_basic
=== CONT  TestAccCCEKubeConfigV3DataSource_basic
--- PASS: TestAccCCEKubeConfigV3DataSource_basic (562.53s)
PASS

Reviewed-by: Anton Sidelnikov
  • Loading branch information
muneeb-jan authored Dec 2, 2024
1 parent 100cba0 commit dde2a0b
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 92 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/jmespath/go-jmespath v0.4.0
github.com/keybase/go-crypto v0.0.0-20200123153347-de78d2cb44f4
github.com/mitchellh/go-homedir v1.1.0
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20241128102934-12bde9d9ad22
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20241129140521-38656d067f56
github.com/unknwon/com v1.0.1
golang.org/x/crypto v0.23.0
golang.org/x/sync v0.1.0
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,8 @@ github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLA
github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce h1:RPclfga2SEJmgMmz2k+Mg7cowZ8yv4Trqw9UsJby758=
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20241125090636-639d0490ef08 h1:wG3Uy3CD7vTtjJmqTl4zgqzwK5RzkgIrFcNBj4W3bRg=
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20241125090636-639d0490ef08/go.mod h1:M1F6OfSRZRzAmAFKQqSLClX952at5hx5rHe4UTEykgg=
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20241128102934-12bde9d9ad22 h1:9TjDvrDqLTS9V5VUuFwi6qx/hp5IaYl+IK9sZuwIf74=
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20241128102934-12bde9d9ad22/go.mod h1:M1F6OfSRZRzAmAFKQqSLClX952at5hx5rHe4UTEykgg=
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20241129140521-38656d067f56 h1:8IBF5eU0C41SlhfzdyjTajMpuhMX5QEpEPN7f4a7zts=
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20241129140521-38656d067f56/go.mod h1:M1F6OfSRZRzAmAFKQqSLClX952at5hx5rHe4UTEykgg=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package acceptance
import (
"fmt"
"testing"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand Down Expand Up @@ -108,7 +109,15 @@ resource "opentelekomcloud_cce_cluster_v3" "cluster_1" {
data "opentelekomcloud_cce_cluster_kubeconfig_v3" "this" {
cluster_id = opentelekomcloud_cce_cluster_v3.cluster_1.id
expiry_date = "2024-02-01"
expiry_date = "%s"
}
`, common.DataSourceSubnet, cceName)
`, common.DataSourceSubnet, cceName, GetDatePlusTwo())
}

// GetDatePlusTwo returns the date for today + 2 days in the YYYY-MM-DD format.
func GetDatePlusTwo() string {
today := time.Now()
futureDate := today.AddDate(0, 0, 2)
formattedDate := futureDate.Format("2006-01-02")
return formattedDate
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,35 @@ import (

const resourceClusterName = "opentelekomcloud_cce_cluster_v3.cluster_1"

func getCceClusterResourceFunc(cfg *cfg.Config, state *terraform.ResourceState) (interface{}, error) {
client, err := cfg.CceV3Client(env.OS_REGION_NAME)
if err != nil {
return nil, fmt.Errorf("error creating CCE v3 Client: %s", err)
}
return clusters.Get(client, state.Primary.ID)
}

func TestAccCCEClusterV3_basic(t *testing.T) {
var cluster clusters.Clusters
rc := common.InitResourceCheck(
resourceClusterName,
&cluster,
getCceClusterResourceFunc,
)
clusterName := randClusterName()
t.Parallel()

quotas.BookOne(t, quotas.CCEClusterQuota)

resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: testAccCheckCCEClusterV3Destroy,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccCCEClusterV3Basic(clusterName),
Check: resource.ComposeTestCheckFunc(
testAccCheckCCEClusterV3Exists(resourceClusterName, &cluster),
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(resourceClusterName, "name", clusterName),
resource.TestCheckResourceAttr(resourceClusterName, "status", "Available"),
resource.TestCheckResourceAttr(resourceClusterName, "cluster_type", "VirtualMachine"),
Expand Down Expand Up @@ -64,19 +78,25 @@ func TestAccCCEClusterV3_basic(t *testing.T) {

func TestAccCCEClusterV3_turbo_basic(t *testing.T) {
var cluster clusters.Clusters
rc := common.InitResourceCheck(
resourceClusterName,
&cluster,
getCceClusterResourceFunc,
)
clusterName := randClusterName()
t.Parallel()

quotas.BookOne(t, quotas.CCEClusterQuota)

resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: testAccCheckCCEClusterV3Destroy,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccCCEClusterTurboV3Basic(clusterName),
Check: resource.ComposeTestCheckFunc(
testAccCheckCCEClusterV3Exists(resourceClusterName, &cluster),
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(resourceClusterName, "name", clusterName),
resource.TestCheckResourceAttr(resourceClusterName, "status", "Available"),
resource.TestCheckResourceAttr(resourceClusterName, "cluster_type", "VirtualMachine"),
Expand All @@ -94,14 +114,20 @@ func TestAccCCEClusterV3_turbo_basic(t *testing.T) {
}

func TestAccCCEClusterV3_importBasic(t *testing.T) {
var cluster clusters.Clusters
rc := common.InitResourceCheck(
resourceClusterName,
&cluster,
getCceClusterResourceFunc,
)
clusterName := randClusterName()
t.Parallel()
quotas.BookOne(t, quotas.CCEClusterQuota)

resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: testAccCheckCCEClusterV3Destroy,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccCCEClusterV3BasicSG(clusterName),
Expand All @@ -121,13 +147,20 @@ func TestAccCCEClusterV3_importBasic(t *testing.T) {
}

func TestAccCCEClusterV3_invalidNetwork(t *testing.T) {
var cluster clusters.Clusters
rc := common.InitResourceCheck(
resourceClusterName,
&cluster,
getCceClusterResourceFunc,
)
clusterName := randClusterName()
t.Parallel()

quotas.BookOne(t, quotas.CCEClusterQuota)
resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: testAccCheckCCEClusterV3Destroy,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccCCEClusterV3InvalidSubnet(clusterName),
Expand All @@ -149,13 +182,20 @@ func TestAccCCEClusterV3_invalidNetwork(t *testing.T) {
}

func TestAccCCEClusterV3_proxyAuth(t *testing.T) {
var cluster clusters.Clusters
rc := common.InitResourceCheck(
resourceClusterName,
&cluster,
getCceClusterResourceFunc,
)
clusterName := randClusterName()
t.Parallel()

quotas.BookOne(t, quotas.CCEClusterQuota)
resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: testAccCheckCCEClusterV3Destroy,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccCCEClusterV3AuthProxy(clusterName),
Expand All @@ -167,19 +207,24 @@ func TestAccCCEClusterV3_proxyAuth(t *testing.T) {

func TestAccCCEClusterV3_timeout(t *testing.T) {
var cluster clusters.Clusters
rc := common.InitResourceCheck(
resourceClusterName,
&cluster,
getCceClusterResourceFunc,
)
clusterName := randClusterName()
t.Parallel()
quotas.BookOne(t, quotas.CCEClusterQuota)

resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: testAccCheckCCEClusterV3Destroy,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccCCEClusterV3Timeout(clusterName),
Check: resource.ComposeTestCheckFunc(
testAccCheckCCEClusterV3Exists(resourceClusterName, &cluster),
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(resourceClusterName, "authentication_mode", "rbac"),
),
},
Expand All @@ -189,19 +234,23 @@ func TestAccCCEClusterV3_timeout(t *testing.T) {

func TestAccCCEClusterV3NoAddons(t *testing.T) {
var cluster clusters.Clusters

rc := common.InitResourceCheck(
resourceClusterName,
&cluster,
getCceClusterResourceFunc,
)
t.Parallel()
quotas.BookOne(t, quotas.CCEClusterQuota)

resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: testAccCheckCCEClusterV3Destroy,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccCCEClusterV3NoAddons(randClusterName()),
Check: resource.ComposeTestCheckFunc(
testAccCheckCCEClusterV3Exists(resourceClusterName, &cluster),
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(resourceClusterName, "installed_addons.#", "0"),
),
},
Expand All @@ -211,19 +260,24 @@ func TestAccCCEClusterV3NoAddons(t *testing.T) {

func TestAccCCEClusterV3NoUserClusterDataSet(t *testing.T) {
var cluster clusters.Clusters

rc := common.InitResourceCheck(
resourceClusterName,
&cluster,
getCceClusterResourceFunc,
)
t.Parallel()

quotas.BookOne(t, quotas.CCEClusterQuota)

resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: testAccCheckCCEClusterV3Destroy,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccCCEClusterV3NoUserClusterData(randClusterName()),
Check: resource.ComposeTestCheckFunc(
testAccCheckCCEClusterV3Exists(resourceClusterName, &cluster),
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(resourceClusterName, "certificate_clusters.#", "0"),
resource.TestCheckResourceAttr(resourceClusterName, "certificate_users.#", "0"),
),
Expand All @@ -232,74 +286,26 @@ func TestAccCCEClusterV3NoUserClusterDataSet(t *testing.T) {
})
}

func testAccCheckCCEClusterV3Destroy(s *terraform.State) error {
config := common.TestAccProvider.Meta().(*cfg.Config)
client, err := config.CceV3Client(env.OS_REGION_NAME)
if err != nil {
return fmt.Errorf("error creating opentelekomcloud CCE client: %s", err)
}

for _, rs := range s.RootModule().Resources {
if rs.Type != "opentelekomcloud_cce_cluster_v3" {
continue
}

_, err := clusters.Get(client, rs.Primary.ID).Extract()
if err == nil {
return fmt.Errorf("cluster still exists")
}
}

return nil
}

func testAccCheckCCEClusterV3Exists(n string, cluster *clusters.Clusters) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("no ID is set")
}

config := common.TestAccProvider.Meta().(*cfg.Config)
client, err := config.CceV3Client(env.OS_REGION_NAME)
if err != nil {
return fmt.Errorf("error creating OpenTelekomCloud CCE client: %s", err)
}

found, err := clusters.Get(client, rs.Primary.ID).Extract()
if err != nil {
return err
}

if found.Metadata.Id != rs.Primary.ID {
return fmt.Errorf("cluster not found")
}

*cluster = *found

return nil
}
}

func TestAccCCEClusterV3_withVersionDiff(t *testing.T) {
var cluster clusters.Clusters
rc := common.InitResourceCheck(
resourceClusterName,
&cluster,
getCceClusterResourceFunc,
)
clusterName := randClusterName()
t.Parallel()
quotas.BookOne(t, quotas.CCEClusterQuota)

resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: testAccCheckCCEClusterV3Destroy,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccCCEClusterV3WithInvalidVersion(clusterName),
Check: resource.ComposeTestCheckFunc(
testAccCheckCCEClusterV3Exists(resourceClusterName, &cluster),
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(resourceClusterName, "name", clusterName),
),
},
Expand Down
4 changes: 2 additions & 2 deletions opentelekomcloud/acceptance/cce/shared/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func deleteSharedCluster(t *testing.T) {
client, err := config.CceV3Client(env.OS_REGION_NAME)
th.AssertNoErr(t, err)

th.AssertNoErr(t, clusters.Delete(client, sharedClusterID).ExtractErr())
th.AssertNoErr(t, clusters.Delete(client, sharedClusterID, clusters.DeleteQueryParams{}))

stateConf := &resource.StateChangeConf{
Pending: []string{"Deleting", "Available", "Unavailable"},
Expand Down Expand Up @@ -137,7 +137,7 @@ func CreateSharedCluster(t *testing.T, client *golangsdk.ServiceClient, subnet *
SubnetId: subnet.ID,
},
},
}).Extract()
})
th.AssertNoErr(t, err)
sharedClusterID = job.Metadata.Id

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func dataSourceCCEClusterKubeConfigV3Read(_ context.Context, d *schema.ResourceD
Duration: duration,
}

kubeconfig, err := clusters.GetCertWithExpiration(client, clusterID, expiryOpts).ExtractMap()
kubeconfig, err := clusters.GetCertWithExpiration(client, clusterID, expiryOpts)
if err != nil {
return fmterr.Errorf("unable to retrieve cluster kubeconfig: %w", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func dataSourceCCEClusterV3Read(_ context.Context, d *schema.ResourceData, meta
return diag.FromErr(err)
}

cert, err := clusters.GetCert(cceClient, d.Id()).Extract()
cert, err := clusters.GetCert(cceClient, d.Id())
if err != nil {
return fmterr.Errorf("error retrieving opentelekomcloud CCE cluster cert: %s", err)
}
Expand Down
Loading

0 comments on commit dde2a0b

Please sign in to comment.