diff --git a/acceptance/openstack/cce/helpers.go b/acceptance/openstack/cce/helpers.go index c49ea9f0e..a2a07edf5 100644 --- a/acceptance/openstack/cce/helpers.go +++ b/acceptance/openstack/cce/helpers.go @@ -4,7 +4,7 @@ import ( "strings" "testing" - "github.com/opentelekomcloud/gophertelekomcloud" + golangsdk "github.com/opentelekomcloud/gophertelekomcloud" "github.com/opentelekomcloud/gophertelekomcloud/acceptance/clients" "github.com/opentelekomcloud/gophertelekomcloud/acceptance/tools" "github.com/opentelekomcloud/gophertelekomcloud/openstack/cce/v3/clusters" @@ -44,14 +44,14 @@ func CreateCluster(t *testing.T, vpcID, subnetID string) string { "kubernetes.io/cpuManagerPolicy": "static", }, }, - }).Extract() + }) th.AssertNoErr(t, err) th.AssertNoErr(t, waitForClusterToActivate(client, cluster.Metadata.Id, 30*60)) return cluster.Metadata.Id } -func CreateTurboCluster(t *testing.T, vpcID, subnetID string, eniSubnetID string, eniCidr string) string { +func CreateTurboCluster(t *testing.T, vpcID, subnetID string, eniSubnetID string, eniCidr string) *clusters.Clusters { client, err := clients.NewCceV3Client() th.AssertNoErr(t, err) @@ -82,17 +82,17 @@ func CreateTurboCluster(t *testing.T, vpcID, subnetID string, eniSubnetID string }, KubernetesSvcIpRange: "10.247.0.0/16", }, - }).Extract() + }) th.AssertNoErr(t, err) th.AssertNoErr(t, waitForClusterToActivate(client, cluster.Metadata.Id, 30*60)) - return cluster.Metadata.Id + return cluster } func DeleteCluster(t *testing.T, clusterID string) { client, err := clients.NewCceV3Client() th.AssertNoErr(t, err) - err = clusters.DeleteWithOpts(client, clusterID, clusters.DeleteOpts{ + err = clusters.Delete(client, clusterID, clusters.DeleteQueryParams{ DeleteEfs: "true", DeleteObs: "true", DeleteSfs: "true", @@ -103,7 +103,7 @@ func DeleteCluster(t *testing.T, clusterID string) { func waitForClusterToActivate(client *golangsdk.ServiceClient, id string, secs int) error { return golangsdk.WaitFor(secs, func() (bool, error) { - cluster, err := clusters.Get(client, id).Extract() + cluster, err := clusters.Get(client, id) if err != nil { return false, err } @@ -119,7 +119,7 @@ func waitForClusterToActivate(client *golangsdk.ServiceClient, id string, secs i func waitForClusterToDelete(client *golangsdk.ServiceClient, id string, secs int) error { return golangsdk.WaitFor(secs, func() (bool, error) { - _, err := clusters.Get(client, id).Extract() + _, err := clusters.Get(client, id) if err != nil { if _, ok := err.(golangsdk.ErrDefault404); ok { return true, nil diff --git a/acceptance/openstack/cce/v3/clusters_test.go b/acceptance/openstack/cce/v3/clusters_test.go index 62cf24cf7..791c81539 100644 --- a/acceptance/openstack/cce/v3/clusters_test.go +++ b/acceptance/openstack/cce/v3/clusters_test.go @@ -5,39 +5,50 @@ import ( "github.com/opentelekomcloud/gophertelekomcloud/acceptance/clients" "github.com/opentelekomcloud/gophertelekomcloud/acceptance/openstack/cce" - "github.com/stretchr/testify/suite" + "github.com/opentelekomcloud/gophertelekomcloud/openstack/cce/v3/clusters" + "github.com/opentelekomcloud/gophertelekomcloud/openstack/networking/v1/subnets" + th "github.com/opentelekomcloud/gophertelekomcloud/testhelper" ) -type testCluster struct { - suite.Suite +func TestListCluster(t *testing.T) { + client, err := clients.NewCceV3Client() + th.AssertNoErr(t, err) - vpcID string - subnetID string - clusterID string - eniSubnetID string - eniCidr string + _, err = clusters.List(client, clusters.ListOpts{}) + th.AssertNoErr(t, err) } func TestCluster(t *testing.T) { - suite.Run(t, new(testCluster)) -} + vpcID := clients.EnvOS.GetEnv("VPC_ID") + if vpcID == "" { + t.Skip("OS_VPC_ID is required for this test") + } + + clientNet, err := clients.NewNetworkV1Client() + th.AssertNoErr(t, err) -func (s *testCluster) SetupSuite() { - t := s.T() - s.vpcID = clients.EnvOS.GetEnv("VPC_ID") - s.subnetID = clients.EnvOS.GetEnv("NETWORK_ID") - s.eniSubnetID = clients.EnvOS.GetEnv("ENI_SUBNET_ID") - s.eniCidr = "10.0.0.0/14" - if s.vpcID == "" || s.subnetID == "" || s.eniSubnetID == "" { - t.Skip("OS_VPC_ID, OS_NETWORK_ID and OS_ENI_SUBNET_ID are required for this test") + listOpts := subnets.ListOpts{ + VpcID: vpcID, } - s.clusterID = cce.CreateTurboCluster(t, s.vpcID, s.subnetID, s.eniSubnetID, s.eniCidr) -} + subnetsList, err := subnets.List(clientNet, listOpts) + th.AssertNoErr(t, err) + + if len(subnetsList) < 1 { + t.Skip("no subnets found in selected VPC") + } + + client, err := clients.NewCceV3Client() + th.AssertNoErr(t, err) + + cluster := cce.CreateTurboCluster(t, vpcID, subnetsList[0].NetworkID, subnetsList[0].SubnetID, subnetsList[0].CIDR) + + clusterID := cluster.Metadata.Id + + clusterGet, err := clusters.Get(client, clusterID) + th.AssertNoErr(t, err) + th.AssertEquals(t, cluster.Metadata.Name, clusterGet.Metadata.Name) -func (s *testCluster) TearDownSuite() { - t := s.T() - if s.clusterID != "" { - cce.DeleteCluster(t, s.clusterID) - s.clusterID = "" + if clusterID != "" { + cce.DeleteCluster(t, clusterID) } } diff --git a/acceptance/openstack/cce/v3/kubeconfig_test.go b/acceptance/openstack/cce/v3/kubeconfig_test.go index 15a0aa5a0..e5bb9831d 100644 --- a/acceptance/openstack/cce/v3/kubeconfig_test.go +++ b/acceptance/openstack/cce/v3/kubeconfig_test.go @@ -8,52 +8,29 @@ import ( "github.com/opentelekomcloud/gophertelekomcloud/openstack/cce/v3/clusters" th "github.com/opentelekomcloud/gophertelekomcloud/testhelper" "github.com/stretchr/testify/require" - "github.com/stretchr/testify/suite" ) -type testKubeConfig struct { - suite.Suite - - routerID string - subnetID string - clusterID string -} - func TestKubeConfig(t *testing.T) { - suite.Run(t, new(testKubeConfig)) -} - -func (s *testKubeConfig) SetupSuite() { - t := s.T() - s.routerID = clients.EnvOS.GetEnv("VPC_ID", "ROUTER_ID") - s.subnetID = clients.EnvOS.GetEnv("NETWORK_ID") - if s.routerID == "" || s.subnetID == "" { + routerID := clients.EnvOS.GetEnv("VPC_ID", "ROUTER_ID") + subnetID := clients.EnvOS.GetEnv("NETWORK_ID") + if routerID == "" || subnetID == "" { t.Skip("OS_ROUTER_ID and OS_NETWORK_ID are required for this test") } - s.clusterID = cce.CreateCluster(t, s.routerID, s.subnetID) -} - -func (s *testKubeConfig) TearDownSuite() { - t := s.T() - if s.clusterID != "" { - cce.DeleteCluster(t, s.clusterID) - s.clusterID = "" - } -} - -func (s *testKubeConfig) TestKubeConfigReading() { - t := s.T() - client, err := clients.NewCceV3Client() th.AssertNoErr(t, err) - kubeConfig, err := clusters.GetCert(client, s.clusterID).ExtractMap() + clusterID := cce.CreateCluster(t, routerID, subnetID) + t.Cleanup(func() { + cce.DeleteCluster(t, clusterID) + }) + + kubeConfig, err := clusters.GetCert(client, clusterID) th.AssertNoErr(t, err) require.NotEmpty(t, kubeConfig) - kubeConfigExp, err := clusters.GetCertWithExpiration(client, s.clusterID, clusters.ExpirationOpts{ + kubeConfigExp, err := clusters.GetCertWithExpiration(client, clusterID, clusters.ExpirationOpts{ Duration: 5, - }).ExtractMap() + }) th.AssertNoErr(t, err) require.NotEmpty(t, kubeConfigExp) } diff --git a/openstack/cce/v3/clusters/Create.go b/openstack/cce/v3/clusters/Create.go new file mode 100644 index 000000000..539bb4407 --- /dev/null +++ b/openstack/cce/v3/clusters/Create.go @@ -0,0 +1,49 @@ +package clusters + +import ( + golangsdk "github.com/opentelekomcloud/gophertelekomcloud" + "github.com/opentelekomcloud/gophertelekomcloud/internal/build" + "github.com/opentelekomcloud/gophertelekomcloud/internal/extract" +) + +// CreateOpts contains all the values needed to create a new cluster +type CreateOpts struct { + // API type, fixed value Cluster + Kind string `json:"kind" required:"true"` + // API version, fixed value v3 + ApiVersion string `json:"apiversion" required:"true"` + // Metadata required to create a cluster + Metadata CreateMetaData `json:"metadata" required:"true"` + // specifications to create a cluster + Spec Spec `json:"spec" required:"true"` +} + +// Metadata required to create a cluster +type CreateMetaData struct { + // Cluster unique name + Name string `json:"name" required:"true"` + // Cluster tag, key/value pair format + Labels map[string]string `json:"labels,omitempty"` + // Cluster annotation, key/value pair format + Annotations map[string]string `json:"annotations,omitempty"` +} + +// Create accepts a CreateOpts struct and uses the values to create a new +// logical cluster. +func Create(client *golangsdk.ServiceClient, opts CreateOpts) (*Clusters, error) { + b, err := build.RequestBody(opts, "") + if err != nil { + return nil, err + } + + // POST /api/v3/projects/{project_id}/clusters + raw, err := client.Post(client.ServiceURL("clusters"), b, nil, &golangsdk.RequestOpts{ + OkCodes: []int{201}, + }) + if err != nil { + return nil, err + } + + var res Clusters + return &res, extract.Into(raw.Body, &res) +} diff --git a/openstack/cce/v3/clusters/Delete.go b/openstack/cce/v3/clusters/Delete.go new file mode 100644 index 000000000..1f95ab3f5 --- /dev/null +++ b/openstack/cce/v3/clusters/Delete.go @@ -0,0 +1,34 @@ +package clusters + +import ( + golangsdk "github.com/opentelekomcloud/gophertelekomcloud" +) + +type DeleteQueryParams struct { + ErrorStatus string `q:"errorStatus,omitempty"` + DeleteEfs string `q:"delete_efs,omitempty"` + DeleteENI string `q:"delete_eni,omitempty"` + DeleteEvs string `q:"delete_evs,omitempty"` + DeleteNet string `q:"delete_net,omitempty"` + DeleteObs string `q:"delete_obs,omitempty"` + DeleteSfs string `q:"delete_sfs,omitempty"` +} + +// Delete will permanently delete a particular cluster based on its unique ID. +func Delete(client *golangsdk.ServiceClient, clusterId string, opts DeleteQueryParams) error { + + url, err := golangsdk.NewURLBuilder().WithEndpoints("clusters", clusterId).WithQueryParams(opts).Build() + if err != nil { + return err + } + _, err = client.Delete(client.ServiceURL(url.String()), &golangsdk.RequestOpts{ + OkCodes: []int{200}, + MoreHeaders: map[string]string{"Content-Type": "application/json"}, + JSONBody: nil, + }) + + if err != nil { + return err + } + return nil +} diff --git a/openstack/cce/v3/clusters/Get.go b/openstack/cce/v3/clusters/Get.go new file mode 100644 index 000000000..eaa229fee --- /dev/null +++ b/openstack/cce/v3/clusters/Get.go @@ -0,0 +1,22 @@ +package clusters + +import ( + golangsdk "github.com/opentelekomcloud/gophertelekomcloud" + "github.com/opentelekomcloud/gophertelekomcloud/internal/extract" +) + +// Get retrieves a particular cluster based on its unique ID. +func Get(client *golangsdk.ServiceClient, clusterId string) (*Clusters, error) { + // GET /api/v3/projects/{project_id}/clusters/{cluster_id} + raw, err := client.Get(client.ServiceURL("clusters", clusterId), nil, &golangsdk.RequestOpts{ + OkCodes: []int{200}, + MoreHeaders: map[string]string{"Content-Type": "application/json"}, + JSONBody: nil, + }) + if err != nil { + return nil, err + } + + var res Clusters + return &res, extract.Into(raw.Body, &res) +} diff --git a/openstack/cce/v3/clusters/GetCert.go b/openstack/cce/v3/clusters/GetCert.go new file mode 100644 index 000000000..4aebba1dd --- /dev/null +++ b/openstack/cce/v3/clusters/GetCert.go @@ -0,0 +1,21 @@ +package clusters + +import ( + golangsdk "github.com/opentelekomcloud/gophertelekomcloud" + "github.com/opentelekomcloud/gophertelekomcloud/internal/extract" +) + +// GetCert retrieves a particular cluster certificate based on its unique ID. (Depreciated) +func GetCert(client *golangsdk.ServiceClient, clusterId string) (*Certificate, error) { + // GET /api/v3/projects/{project_id}/clusters/{cluster_id}/clustercert + raw, err := client.Get(client.ServiceURL("clusters", clusterId, "clustercert"), nil, &golangsdk.RequestOpts{ + OkCodes: []int{200}, + MoreHeaders: map[string]string{"Content-Type": "application/json"}, + }) + if err != nil { + return nil, err + } + + var res Certificate + return &res, extract.Into(raw.Body, &res) +} diff --git a/openstack/cce/v3/clusters/GetCertWithExpiration.go b/openstack/cce/v3/clusters/GetCertWithExpiration.go new file mode 100644 index 000000000..ec7f59c3a --- /dev/null +++ b/openstack/cce/v3/clusters/GetCertWithExpiration.go @@ -0,0 +1,31 @@ +package clusters + +import ( + golangsdk "github.com/opentelekomcloud/gophertelekomcloud" + "github.com/opentelekomcloud/gophertelekomcloud/internal/build" + "github.com/opentelekomcloud/gophertelekomcloud/internal/extract" +) + +type ExpirationOpts struct { + Duration int `json:"duration" required:"true"` +} + +// GetCertWithExpiration retrieves a particular cluster certificate based on its unique ID. +func GetCertWithExpiration(client *golangsdk.ServiceClient, clusterId string, opts ExpirationOpts) (*Certificate, error) { + b, err := build.RequestBody(opts, "") + if err != nil { + return nil, err + } + + // POST /api/v3/projects/{project_id}/clusters/{cluster_id}/clustercert + raw, err := client.Post(client.ServiceURL("clusters", clusterId, "clustercert"), b, nil, &golangsdk.RequestOpts{ + OkCodes: []int{200}, + MoreHeaders: map[string]string{"Content-Type": "application/json"}, + }) + if err != nil { + return nil, err + } + + var res Certificate + return &res, extract.Into(raw.Body, &res) +} diff --git a/openstack/cce/v3/clusters/List.go b/openstack/cce/v3/clusters/List.go new file mode 100644 index 000000000..fd62f8f0c --- /dev/null +++ b/openstack/cce/v3/clusters/List.go @@ -0,0 +1,76 @@ +package clusters + +import ( + golangsdk "github.com/opentelekomcloud/gophertelekomcloud" + "github.com/opentelekomcloud/gophertelekomcloud/internal/extract" +) + +// ListOpts allows the filtering of list data using given parameters. +type ListOpts struct { + Name string `json:"name"` + ID string `json:"uuid"` + Type string `json:"type"` + VpcID string `json:"vpc"` + Phase string `json:"phase"` +} + +// List returns collection of clusters. +func List(client *golangsdk.ServiceClient, opts ListOpts) ([]Clusters, error) { + // GET /api/v3/projects/{project_id}/clusters + raw, err := client.Get(client.ServiceURL("clusters"), nil, &golangsdk.RequestOpts{ + OkCodes: []int{200}, + MoreHeaders: map[string]string{"Content-Type": "application/json"}, + JSONBody: nil, + }) + if err != nil { + return nil, err + } + + var res ListCluster + err = extract.Into(raw.Body, &res) + if err != nil { + return nil, err + } + + return FilterClusters(res.Clusters, opts), nil +} + +type ListCluster struct { + // API type, fixed value Cluster + Kind string `json:"kind"` + // API version, fixed value v3 + ApiVersion string `json:"apiVersion"` + // all Clusters + Clusters []Clusters `json:"items"` +} + +func FilterClusters(clusters []Clusters, opts ListOpts) []Clusters { + var refinedClusters []Clusters + + for _, cluster := range clusters { + if matchesFilters(cluster, opts) { + refinedClusters = append(refinedClusters, cluster) + } + } + + return refinedClusters +} + +func matchesFilters(cluster Clusters, opts ListOpts) bool { + if opts.Name != "" && cluster.Metadata.Name != opts.Name { + return false + } + if opts.ID != "" && cluster.Metadata.Id != opts.ID { + return false + } + if opts.Type != "" && cluster.Spec.Type != opts.Type { + return false + } + if opts.VpcID != "" && cluster.Spec.HostNetwork.VpcId != opts.VpcID { + return false + } + if opts.Phase != "" && cluster.Status.Phase != opts.Phase { + return false + } + return true +} diff --git a/openstack/cce/v3/clusters/Update.go b/openstack/cce/v3/clusters/Update.go new file mode 100644 index 000000000..74ee63cc2 --- /dev/null +++ b/openstack/cce/v3/clusters/Update.go @@ -0,0 +1,36 @@ +package clusters + +import ( + golangsdk "github.com/opentelekomcloud/gophertelekomcloud" + "github.com/opentelekomcloud/gophertelekomcloud/internal/build" + "github.com/opentelekomcloud/gophertelekomcloud/internal/extract" +) + +// UpdateOpts contains all the values needed to update a new cluster +type UpdateOpts struct { + Spec UpdateSpec `json:"spec" required:"true"` +} + +type UpdateSpec struct { + // Cluster description + Description string `json:"description,omitempty"` +} + +// Update allows clusters to update description. +func Update(client *golangsdk.ServiceClient, clusterId string, opts UpdateOpts) (*Clusters, error) { + b, err := build.RequestBody(opts, "") + if err != nil { + return nil, err + } + + // PUT /api/v3/projects/{project_id}/clusters/{cluster_id} + raw, err := client.Put(client.ServiceURL("clusters", clusterId), b, nil, &golangsdk.RequestOpts{ + OkCodes: []int{200}, + }) + if err != nil { + return nil, err + } + + var res Clusters + return &res, extract.Into(raw.Body, &res) +} diff --git a/openstack/cce/v3/clusters/UpdateMasterIP.go b/openstack/cce/v3/clusters/UpdateMasterIP.go new file mode 100644 index 000000000..4afcfa1a3 --- /dev/null +++ b/openstack/cce/v3/clusters/UpdateMasterIP.go @@ -0,0 +1,34 @@ +package clusters + +import ( + golangsdk "github.com/opentelekomcloud/gophertelekomcloud" + "github.com/opentelekomcloud/gophertelekomcloud/internal/build" +) + +type UpdateIpOpts struct { + Action string `json:"action" required:"true"` + Spec IpSpec `json:"spec,omitempty"` + ElasticIp string `json:"elasticIp"` +} + +type IpSpec struct { + ID string `json:"id" required:"true"` +} + +// Update the access information of a specified cluster. +func UpdateMasterIp(client *golangsdk.ServiceClient, clusterId string, opts UpdateIpOpts) error { + b, err := build.RequestBody(opts, "") + if err != nil { + return err + } + + // PUT /api/v3/projects/{project_id}/clusters/{cluster_id}/mastereip + _, err = client.Put(client.ServiceURL("clusters", clusterId, "mastereip"), b, nil, &golangsdk.RequestOpts{ + OkCodes: []int{200}, + }) + if err != nil { + return err + } + + return nil +} diff --git a/openstack/cce/v3/clusters/results.go b/openstack/cce/v3/clusters/common.go similarity index 75% rename from openstack/cce/v3/clusters/results.go rename to openstack/cce/v3/clusters/common.go index 6f038be3a..ce2af75ab 100644 --- a/openstack/cce/v3/clusters/results.go +++ b/openstack/cce/v3/clusters/common.go @@ -1,19 +1,6 @@ package clusters -import ( - "encoding/json" - - "github.com/opentelekomcloud/gophertelekomcloud" -) - -type ListCluster struct { - // API type, fixed value Cluster - Kind string `json:"kind"` - // API version, fixed value v3 - ApiVersion string `json:"apiVersion"` - // all Clusters - Clusters []Clusters `json:"items"` -} +import "encoding/json" type Clusters struct { // API type, fixed value Cluster @@ -75,6 +62,19 @@ type Spec struct { EnableMasterVolumeEncryption *bool `json:"enableMasterVolumeEncryption,omitempty"` } +type Status struct { + // The state of the cluster + Phase string `json:"phase"` + // The ID of the Job that is operating asynchronously in the cluster + JobID string `json:"jobID"` + // Reasons for the cluster to become current + Reason string `json:"reason"` + // The status of each component in the cluster + Conditions Conditions `json:"conditions"` + // Kube-apiserver access address in the cluster + Endpoints []Endpoints `json:"-"` +} + // Node network parameters type HostNetworkSpec struct { // The ID of the VPC used to create the node @@ -110,19 +110,6 @@ type AuthenticationSpec struct { AuthenticatingProxy map[string]string `json:"authenticatingProxy" required:"true"` } -type Status struct { - // The state of the cluster - Phase string `json:"phase"` - // The ID of the Job that is operating asynchronously in the cluster - JobID string `json:"jobID"` - // Reasons for the cluster to become current - Reason string `json:"reason"` - // The status of each component in the cluster - Conditions Conditions `json:"conditions"` - // Kube-apiserver access address in the cluster - Endpoints []Endpoints `json:"-"` -} - type Conditions struct { // The type of component Type string `json:"type"` @@ -171,7 +158,7 @@ type CertCluster struct { // Server IP address Server string `json:"server"` // Certificate data - CertAuthorityData string `json:"certificate-authority-data"` + CertAuthorityData string `json:"certificate-authority-data,omitempty"` } type CertUsers struct { @@ -240,81 +227,3 @@ func (r *Status) UnmarshalJSON(b []byte) error { return err } - -type commonResult struct { - golangsdk.Result -} - -// Extract is a function that accepts a result and extracts a cluster. -func (r commonResult) Extract() (*Clusters, error) { - var s Clusters - err := r.ExtractInto(&s) - return &s, err -} - -// ExtractCluster is a function that accepts a ListOpts struct, which allows you to filter and sort -// the returned collection for greater efficiency. -func (r commonResult) ExtractClusters() ([]Clusters, error) { - var s ListCluster - err := r.ExtractInto(&s) - if err != nil { - return nil, err - } - - return s.Clusters, nil - -} - -// CreateResult represents the result of a create operation. Call its Extract -// method to interpret it as a Cluster. -type CreateResult struct { - commonResult -} - -// GetResult represents the result of a get operation. Call its Extract -// method to interpret it as a Cluster. -type GetResult struct { - commonResult -} - -// UpdateResult represents the result of an update operation. Call its Extract -// method to interpret it as a Cluster. -type UpdateResult struct { - commonResult -} - -// DeleteResult represents the result of a delete operation. Call its ExtractErr -// method to determine if the request succeeded or failed. -type DeleteResult struct { - golangsdk.ErrResult -} - -// ListResult represents the result of a list operation. Call its ExtractCluster -// method to interpret it as a Cluster. -type ListResult struct { - commonResult -} - -type GetCertResult struct { - golangsdk.Result -} - -// Extract is a function that accepts a result and extracts a cluster. -func (r GetCertResult) Extract() (*Certificate, error) { - var s Certificate - err := r.ExtractInto(&s) - return &s, err -} - -// ExtractMap is a function that accepts a result and extracts a kubeconfig. -func (r GetCertResult) ExtractMap() (map[string]interface{}, error) { - var s map[string]interface{} - err := r.ExtractInto(&s) - return s, err -} - -// UpdateIpResult represents the result of an update operation. Call its Extract -// method to interpret it as a Cluster. -type UpdateIpResult struct { - golangsdk.ErrResult -} diff --git a/openstack/cce/v3/clusters/doc.go b/openstack/cce/v3/clusters/doc.go deleted file mode 100644 index 504375363..000000000 --- a/openstack/cce/v3/clusters/doc.go +++ /dev/null @@ -1,55 +0,0 @@ -/* -Package Clusters enables management and retrieval of Clusters -CCE service. - -Example to List Clusters - - listOpts:=clusters.ListOpts{} - allClusters,err:=clusters.List(client,listOpts) - if err != nil { - panic(err) - } - - for _, cluster := range allClusters { - fmt.Printf("%+v\n", cluster) - } - -Example to Create a cluster - - createOpts:=clusters.CreateOpts{Kind:"Cluster", - ApiVersion:"v3", - Metadata:clusters.CreateMetaData{Name:"test-cluster"}, - Spec:clusters.Spec{Type: "VirtualMachine", - Flavor: "cce.s1.small", - Version:"v1.7.3-r10", - HostNetwork:clusters.HostNetworkSpec{VpcId:"3b9740a0-b44d-48f0-84ee-42eb166e54f7", - SubnetId:"3e8e5957-649f-477b-9e5b-f1f75b21c045",}, - ContainerNetwork:clusters.ContainerNetworkSpec{Mode:"overlay_l2"}, - }, - } - cluster,err := clusters.Create(client,createOpts).Extract() - if err != nil { - panic(err) - } - -Example to Update a cluster - - updateOpts := clusters.UpdateOpts{Spec:clusters.UpdateSpec{Description:"test"}} - - clusterID := "4e8e5957-649f-477b-9e5b-f1f75b21c03c" - - cluster,err := clusters.Update(client,clusterID,updateOpts).Extract() - if err != nil { - panic(err) - } - -Example to Delete a cluster - - clusterID := "4e8e5957-649f-477b-9e5b-f1f75b21c03c" - - err := clusters.Delete(client,clusterID).ExtractErr() - if err != nil { - panic(err) - } -*/ -package clusters diff --git a/openstack/cce/v3/clusters/requests.go b/openstack/cce/v3/clusters/requests.go deleted file mode 100644 index 06bd0ea5e..000000000 --- a/openstack/cce/v3/clusters/requests.go +++ /dev/null @@ -1,283 +0,0 @@ -package clusters - -import ( - "reflect" - - "github.com/opentelekomcloud/gophertelekomcloud" -) - -var RequestOpts = golangsdk.RequestOpts{ - MoreHeaders: map[string]string{"Content-Type": "application/json"}, -} - -// ListOpts allows the filtering of list data using given parameters. -type ListOpts struct { - Name string `json:"name"` - ID string `json:"uuid"` - Type string `json:"type"` - VpcID string `json:"vpc"` - Phase string `json:"phase"` -} - -// List returns collection of clusters. -func List(client *golangsdk.ServiceClient, opts ListOpts) ([]Clusters, error) { - var r ListResult - _, r.Err = client.Get(rootURL(client), &r.Body, &golangsdk.RequestOpts{ - OkCodes: []int{200}, - MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil, - }) - - allClusters, err := r.ExtractClusters() - if err != nil { - return nil, err - } - - return FilterClusters(allClusters, opts), nil -} - -func FilterClusters(clusters []Clusters, opts ListOpts) []Clusters { - - var refinedClusters []Clusters - var matched bool - m := map[string]FilterStruct{} - - if opts.Name != "" { - m["Name"] = FilterStruct{Value: opts.Name, Driller: []string{"Metadata"}} - } - if opts.ID != "" { - m["Id"] = FilterStruct{Value: opts.ID, Driller: []string{"Metadata"}} - } - if opts.Type != "" { - m["Type"] = FilterStruct{Value: opts.Type, Driller: []string{"Spec"}} - } - if opts.VpcID != "" { - m["VpcId"] = FilterStruct{Value: opts.VpcID, Driller: []string{"Spec", "HostNetwork"}} - } - if opts.Phase != "" { - m["Phase"] = FilterStruct{Value: opts.Phase, Driller: []string{"Status"}} - } - - if len(m) > 0 && len(clusters) > 0 { - for _, cluster := range clusters { - matched = true - - for key, value := range m { - if sVal := GetStructNestedField(&cluster, key, value.Driller); !(sVal == value.Value) { - matched = false - } - } - if matched { - refinedClusters = append(refinedClusters, cluster) - } - } - - } else { - refinedClusters = clusters - } - - return refinedClusters -} - -type FilterStruct struct { - Value string - Driller []string -} - -func GetStructNestedField(v *Clusters, field string, structDriller []string) string { - r := reflect.ValueOf(v) - for _, drillField := range structDriller { - f := reflect.Indirect(r).FieldByName(drillField).Interface() - r = reflect.ValueOf(f) - } - f1 := reflect.Indirect(r).FieldByName(field) - return f1.String() -} - -// CreateOptsBuilder allows extensions to add additional parameters to the -// Create request. -type CreateOptsBuilder interface { - ToClusterCreateMap() (map[string]interface{}, error) -} - -// CreateOpts contains all the values needed to create a new cluster -type CreateOpts struct { - // API type, fixed value Cluster - Kind string `json:"kind" required:"true"` - // API version, fixed value v3 - ApiVersion string `json:"apiversion" required:"true"` - // Metadata required to create a cluster - Metadata CreateMetaData `json:"metadata" required:"true"` - // specifications to create a cluster - Spec Spec `json:"spec" required:"true"` -} - -// Metadata required to create a cluster -type CreateMetaData struct { - // Cluster unique name - Name string `json:"name" required:"true"` - // Cluster tag, key/value pair format - Labels map[string]string `json:"labels,omitempty"` - // Cluster annotation, key/value pair format - Annotations map[string]string `json:"annotations,omitempty"` -} - -// ToClusterCreateMap builds a create request body from CreateOpts. -func (opts CreateOpts) ToClusterCreateMap() (map[string]interface{}, error) { - return golangsdk.BuildRequestBody(opts, "") -} - -type ExpirationOptsBuilder interface { - ToExpirationGetMap() (map[string]interface{}, error) -} - -type ExpirationOpts struct { - Duration int `json:"duration" required:"true"` -} - -func (opts ExpirationOpts) ToExpirationGetMap() (map[string]interface{}, error) { - return golangsdk.BuildRequestBody(opts, "") -} - -// Create accepts a CreateOpts struct and uses the values to create a new -// logical cluster. -func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { - b, err := opts.ToClusterCreateMap() - if err != nil { - r.Err = err - return - } - reqOpt := &golangsdk.RequestOpts{OkCodes: []int{201}} - _, r.Err = c.Post(rootURL(c), b, &r.Body, reqOpt) - return -} - -// Get retrieves a particular cluster based on its unique ID. -func Get(c *golangsdk.ServiceClient, id string) (r GetResult) { - _, r.Err = c.Get(resourceURL(c, id), &r.Body, &golangsdk.RequestOpts{ - OkCodes: []int{200}, - MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil, - }) - return -} - -// GetCert retrieves a particular cluster certificate based on its unique ID. -func GetCert(c *golangsdk.ServiceClient, id string) (r GetCertResult) { - _, r.Err = c.Get(certificateURL(c, id), &r.Body, &golangsdk.RequestOpts{ - OkCodes: []int{200}, - MoreHeaders: RequestOpts.MoreHeaders, - }) - return -} - -// GetCertWithExpiration retrieves a particular cluster certificate based on its unique ID. -func GetCertWithExpiration(c *golangsdk.ServiceClient, id string, opts ExpirationOptsBuilder) (r GetCertResult) { - b, err := opts.ToExpirationGetMap() - if err != nil { - r.Err = err - return - } - - _, r.Err = c.Post(certificateURL(c, id), b, &r.Body, &golangsdk.RequestOpts{ - OkCodes: []int{200}, - MoreHeaders: RequestOpts.MoreHeaders, - }) - return -} - -// UpdateOpts contains all the values needed to update a new cluster -type UpdateOpts struct { - Spec UpdateSpec `json:"spec" required:"true"` -} - -type UpdateSpec struct { - // Cluster description - Description string `json:"description,omitempty"` -} - -// UpdateOptsBuilder allows extensions to add additional parameters to the -// Update request. -type UpdateOptsBuilder interface { - ToClusterUpdateMap() (map[string]interface{}, error) -} - -// ToClusterUpdateMap builds an update body based on UpdateOpts. -func (opts UpdateOpts) ToClusterUpdateMap() (map[string]interface{}, error) { - return golangsdk.BuildRequestBody(opts, "") -} - -// Update allows clusters to update description. -func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { - b, err := opts.ToClusterUpdateMap() - if err != nil { - r.Err = err - return - } - _, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{ - OkCodes: []int{200}, - }) - return -} - -// Delete will permanently delete a particular cluster based on its unique ID. -func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) { - _, r.Err = c.Delete(resourceURL(c, id), &golangsdk.RequestOpts{ - OkCodes: []int{200}, - MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil, - }) - return -} - -type DeleteOpts struct { - ErrorStatus string `q:"errorStatus"` - DeleteEfs string `q:"delete_efs"` - DeleteENI string `q:"delete_eni"` - DeleteEvs string `q:"delete_evs"` - DeleteNet string `q:"delete_net"` - DeleteObs string `q:"delete_obs"` - DeleteSfs string `q:"delete_sfs"` -} - -func DeleteWithOpts(c *golangsdk.ServiceClient, id string, opts DeleteOpts) error { - url := resourceURL(c, id) - q, err := golangsdk.BuildQueryString(&opts) - if err != nil { - return err - } - - _, err = c.Delete(url+q.String(), &golangsdk.RequestOpts{ - OkCodes: []int{200}, - MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil, - }) - return err -} - -type UpdateIpOpts struct { - Action string `json:"action" required:"true"` - Spec IpSpec `json:"spec,omitempty"` - ElasticIp string `json:"elasticIp"` -} - -type IpSpec struct { - ID string `json:"id" required:"true"` -} - -type UpdateIpOptsBuilder interface { - ToMasterIpUpdateMap() (map[string]interface{}, error) -} - -func (opts UpdateIpOpts) ToMasterIpUpdateMap() (map[string]interface{}, error) { - return golangsdk.BuildRequestBody(opts, "spec") -} - -// Update the access information of a specified cluster. -func UpdateMasterIp(c *golangsdk.ServiceClient, id string, opts UpdateIpOptsBuilder) (r UpdateIpResult) { - b, err := opts.ToMasterIpUpdateMap() - if err != nil { - r.Err = err - return - } - _, r.Err = c.Put(masterIpURL(c, id), b, &r.Body, &golangsdk.RequestOpts{ - OkCodes: []int{200}, - }) - return -} diff --git a/openstack/cce/v3/clusters/testing/requests_test.go b/openstack/cce/v3/clusters/testing/requests_test.go index a2b8ffbd1..bb4f4132b 100644 --- a/openstack/cce/v3/clusters/testing/requests_test.go +++ b/openstack/cce/v3/clusters/testing/requests_test.go @@ -22,7 +22,7 @@ func TestGetV3Cluster(t *testing.T) { _, _ = fmt.Fprint(w, Output) }) - actual, err := clusters.Get(fake.ServiceClient(), "daa97872-59d7-11e8-a787-0255ac101f54").Extract() + actual, err := clusters.Get(fake.ServiceClient(), "daa97872-59d7-11e8-a787-0255ac101f54") th.AssertNoErr(t, err) expected := Expected th.AssertDeepEquals(t, expected, actual) @@ -41,7 +41,7 @@ func TestGetV3ClusterOTC(t *testing.T) { _, _ = fmt.Fprint(w, OutputOTC) }) - actual, err := clusters.Get(fake.ServiceClient(), "daa97872-59d7-11e8-a787-0255ac101f54").Extract() + actual, err := clusters.Get(fake.ServiceClient(), "daa97872-59d7-11e8-a787-0255ac101f54") th.AssertNoErr(t, err) expected := ExpectedOTC th.AssertDeepEquals(t, expected, actual) @@ -158,7 +158,7 @@ func TestCreateV3Cluster(t *testing.T) { AuthenticatingProxy: make(map[string]string)}, }, } - actual, err := clusters.Create(fake.ServiceClient(), options).Extract() + actual, err := clusters.Create(fake.ServiceClient(), options) th.AssertNoErr(t, err) expected := Expected th.AssertDeepEquals(t, expected, actual) @@ -231,7 +231,7 @@ func TestCreateV3TurboCluster(t *testing.T) { AuthenticatingProxy: make(map[string]string)}, }, } - actual, err := clusters.Create(fake.ServiceClient(), options).Extract() + actual, err := clusters.Create(fake.ServiceClient(), options) th.AssertNoErr(t, err) expected := Expected th.AssertDeepEquals(t, expected, actual) @@ -261,7 +261,7 @@ func TestUpdateV3Cluster(t *testing.T) { _, _ = fmt.Fprint(w, Output) }) options := clusters.UpdateOpts{Spec: clusters.UpdateSpec{Description: "new description"}} - actual, err := clusters.Update(fake.ServiceClient(), "daa97872-59d7-11e8-a787-0255ac101f54", options).Extract() + actual, err := clusters.Update(fake.ServiceClient(), "daa97872-59d7-11e8-a787-0255ac101f54", options) th.AssertNoErr(t, err) expected := Expected th.AssertDeepEquals(t, expected, actual) @@ -277,7 +277,7 @@ func TestDeleteV3Cluster(t *testing.T) { w.WriteHeader(http.StatusOK) }) - err := clusters.Delete(fake.ServiceClient(), "daa97872-59d7-11e8-a787-0255ac101f54").ExtractErr() + err := clusters.Delete(fake.ServiceClient(), "daa97872-59d7-11e8-a787-0255ac101f54", clusters.DeleteQueryParams{}) th.AssertNoErr(t, err) } @@ -292,7 +292,7 @@ func TestDeleteV3TurboCluster(t *testing.T) { w.WriteHeader(http.StatusOK) }) - err := clusters.Delete(fake.ServiceClient(), "daa97872-59d7-11e8-a787-0255ac101f54").ExtractErr() + err := clusters.Delete(fake.ServiceClient(), "daa97872-59d7-11e8-a787-0255ac101f54", clusters.DeleteQueryParams{}) th.AssertNoErr(t, err) } diff --git a/openstack/cce/v3/clusters/urls.go b/openstack/cce/v3/clusters/urls.go deleted file mode 100644 index 8da26b8a9..000000000 --- a/openstack/cce/v3/clusters/urls.go +++ /dev/null @@ -1,25 +0,0 @@ -package clusters - -import "github.com/opentelekomcloud/gophertelekomcloud" - -const ( - rootPath = "clusters" - certPath = "clustercert" - masterIpPath = "mastereip" -) - -func rootURL(client *golangsdk.ServiceClient) string { - return client.ServiceURL(rootPath) -} - -func resourceURL(c *golangsdk.ServiceClient, id string) string { - return c.ServiceURL(rootPath, id) -} - -func certificateURL(c *golangsdk.ServiceClient, id string) string { - return c.ServiceURL(rootPath, id, certPath) -} - -func masterIpURL(c *golangsdk.ServiceClient, id string) string { - return c.ServiceURL(rootPath, id, masterIpPath) -}