github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/acceptance/openstack/rds/v3/helpers.go (about)

     1  package v3
     2  
     3  import (
     4  	"testing"
     5  
     6  	golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
     7  	"github.com/opentelekomcloud/gophertelekomcloud/acceptance/clients"
     8  	"github.com/opentelekomcloud/gophertelekomcloud/acceptance/openstack"
     9  	"github.com/opentelekomcloud/gophertelekomcloud/acceptance/tools"
    10  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/rds/v3/configurations"
    11  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/rds/v3/instances"
    12  	th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
    13  )
    14  
    15  func CreateRDS(t *testing.T, client *golangsdk.ServiceClient, region string) *instances.Instance {
    16  	t.Logf("Attempting to create RDSv3")
    17  
    18  	rdsName := tools.RandomString("rds-test-", 8)
    19  
    20  	az := clients.EnvOS.GetEnv("AVAILABILITY_ZONE")
    21  	if az == "" {
    22  		az = "eu-de-01"
    23  	}
    24  
    25  	vpcID := clients.EnvOS.GetEnv("VPC_ID")
    26  	subnetID := clients.EnvOS.GetEnv("NETWORK_ID")
    27  	kmsID := clients.EnvOS.GetEnv("KMS_ID")
    28  	if vpcID == "" || subnetID == "" {
    29  		t.Skip("One of OS_VPC_ID or OS_NETWORK_ID env vars is missing but RDS test requires using existing network")
    30  	}
    31  
    32  	createRdsOpts := instances.CreateRdsOpts{
    33  		Name:             rdsName,
    34  		Port:             "8635",
    35  		Password:         "acc-test-password1!",
    36  		FlavorRef:        "rds.pg.c2.medium",
    37  		Region:           region,
    38  		AvailabilityZone: az,
    39  		VpcId:            vpcID,
    40  		SubnetId:         subnetID,
    41  		SecurityGroupId:  openstack.DefaultSecurityGroup(t),
    42  		DiskEncryptionId: kmsID,
    43  
    44  		Volume: &instances.Volume{
    45  			Type: "COMMON",
    46  			Size: 100,
    47  		},
    48  		Datastore: &instances.Datastore{
    49  			Type:    "PostgreSQL",
    50  			Version: "11",
    51  		},
    52  		UnchangeableParam: &instances.Param{
    53  			LowerCaseTableNames: "0",
    54  		},
    55  	}
    56  
    57  	rds, err := instances.Create(client, createRdsOpts)
    58  	th.AssertNoErr(t, err)
    59  	err = instances.WaitForJobCompleted(client, 1200, rds.JobId)
    60  	th.AssertNoErr(t, err)
    61  	t.Logf("Created RDSv3: %s", rds.Instance.Id)
    62  
    63  	return &rds.Instance
    64  }
    65  
    66  func CreateMySqlRDS(t *testing.T, client *golangsdk.ServiceClient, region string) *instances.Instance {
    67  	t.Logf("Attempting to create RDSv3")
    68  
    69  	rdsName := tools.RandomString("rds-test-", 8)
    70  
    71  	az := clients.EnvOS.GetEnv("AVAILABILITY_ZONE")
    72  	if az == "" {
    73  		az = "eu-de-01"
    74  	}
    75  
    76  	vpcID := clients.EnvOS.GetEnv("VPC_ID")
    77  	subnetID := clients.EnvOS.GetEnv("NETWORK_ID")
    78  	if vpcID == "" || subnetID == "" {
    79  		t.Skip("One of OS_VPC_ID or OS_NETWORK_ID env vars is missing but RDS test requires using existing network")
    80  	}
    81  
    82  	createRdsOpts := instances.CreateRdsOpts{
    83  		Name:             rdsName,
    84  		Port:             "8635",
    85  		Password:         "acc-test-password1!",
    86  		FlavorRef:        "rds.mysql.c2.medium",
    87  		Region:           region,
    88  		AvailabilityZone: az,
    89  		VpcId:            vpcID,
    90  		SubnetId:         subnetID,
    91  		SecurityGroupId:  openstack.DefaultSecurityGroup(t),
    92  
    93  		Volume: &instances.Volume{
    94  			Type: "COMMON",
    95  			Size: 100,
    96  		},
    97  		Datastore: &instances.Datastore{
    98  			Type:    "MySQL",
    99  			Version: "8.0",
   100  		},
   101  	}
   102  
   103  	rds, err := instances.Create(client, createRdsOpts)
   104  	th.AssertNoErr(t, err)
   105  	err = instances.WaitForJobCompleted(client, 1200, rds.JobId)
   106  	th.AssertNoErr(t, err)
   107  	t.Logf("Created RDSv3: %s", rds.Instance.Id)
   108  
   109  	return &rds.Instance
   110  }
   111  
   112  func DeleteRDS(t *testing.T, client *golangsdk.ServiceClient, rdsID string) {
   113  	t.Logf("Attempting to delete RDSv3: %s", rdsID)
   114  
   115  	err := golangsdk.WaitFor(1000, func() (bool, error) {
   116  		_, err := instances.Delete(client, rdsID)
   117  		if err != nil {
   118  			return false, nil
   119  		}
   120  		return true, nil
   121  	})
   122  	th.AssertNoErr(t, err)
   123  
   124  	t.Logf("RDSv3 instance deleted: %s", rdsID)
   125  }
   126  
   127  func updateRDS(t *testing.T, client *golangsdk.ServiceClient, rdsID string) error {
   128  	t.Logf("Attempting to increase volume size")
   129  
   130  	t.Logf("Update volume size could be done only in status `available`")
   131  	if err := instances.WaitForStateAvailable(client, 600, rdsID); err != nil {
   132  		t.Fatalf("Status available wasn't present")
   133  	}
   134  
   135  	updateOpts := instances.EnlargeVolumeRdsOpts{
   136  		InstanceId: rdsID,
   137  		Size:       200,
   138  	}
   139  
   140  	updateResult, err := instances.EnlargeVolume(client, updateOpts)
   141  	th.AssertNoErr(t, err)
   142  
   143  	if err := instances.WaitForJobCompleted(client, 1200, *updateResult); err != nil {
   144  		return err
   145  	}
   146  	t.Logf("RDSv3 successfully updated: %s", rdsID)
   147  	return nil
   148  }
   149  
   150  func createRDSConfiguration(t *testing.T, client *golangsdk.ServiceClient) *configurations.Configuration {
   151  	t.Logf("Attempting to create RDSv3 template configuration")
   152  	prefix := "rds-config-"
   153  	configName := tools.RandomString(prefix, 3)
   154  
   155  	configCreateOpts := configurations.CreateOpts{
   156  		Name:        configName,
   157  		Description: "some config description",
   158  		Values: map[string]string{
   159  			"autocommit": "OFF",
   160  		},
   161  		DataStore: configurations.DataStore{
   162  			Type:    "PostgreSQL",
   163  			Version: "11",
   164  		},
   165  	}
   166  
   167  	rdsConfiguration, err := configurations.Create(client, configCreateOpts)
   168  	th.AssertNoErr(t, err)
   169  
   170  	t.Logf("Created RDSv3 configuration: %s", rdsConfiguration.ID)
   171  
   172  	return rdsConfiguration
   173  }
   174  
   175  func deleteRDSConfiguration(t *testing.T, client *golangsdk.ServiceClient, rdsConfigID string) {
   176  	t.Logf("Attempting to delete RDSv3 configuration: %s", rdsConfigID)
   177  
   178  	err := configurations.Delete(client, rdsConfigID)
   179  	th.AssertNoErr(t, err)
   180  
   181  	t.Logf("RDSv3 configuration deleted: %s", rdsConfigID)
   182  }
   183  
   184  func updateRDSConfiguration(t *testing.T, client *golangsdk.ServiceClient, rdsConfigID string) {
   185  	t.Logf("Attempting to update name")
   186  
   187  	prefix := "rds-update-config-"
   188  	configName := tools.RandomString(prefix, 3)
   189  
   190  	updateOpts := configurations.UpdateOpts{
   191  		ConfigId:    rdsConfigID,
   192  		Name:        configName,
   193  		Description: "some updated description",
   194  		Values: map[string]string{
   195  			"autocommit": "ON",
   196  		},
   197  	}
   198  
   199  	err := configurations.Update(client, updateOpts)
   200  	th.AssertNoErr(t, err)
   201  
   202  	t.Logf("RDSv3 configuration updated")
   203  }