Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataArts API implementation #651

Closed
wants to merge 14 commits into from
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ test-unit:
test-acc:
@echo "Starting acceptance tests..."
@go test ./acceptance/... -race -covermode=atomic -coverprofile=coverage.txt -timeout 20m -v

test-case:
go test -v github.com/opentelekomcloud/gophertelekomcloud/acceptance/openstack/$(scope) -run $(case)
4 changes: 2 additions & 2 deletions acceptance/openstack/dataarts/v1.1/clusters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestDataArtsClusterLifecycle(t *testing.T) {
},
}

createResp, err := cluster.Create(client, createOpts)
createResp, err := cluster.Create(client, createOpts, "en")
th.AssertNoErr(t, err)
tools.PrintResource(t, createResp)

Expand Down Expand Up @@ -86,7 +86,7 @@ func waitForStateAvailable(client *golangsdk.ServiceClient, secs int, instanceID
func deleteDataArts(t *testing.T, client *golangsdk.ServiceClient, instanceId string) {
t.Logf("Attempting to delete DataArts instance: %s", instanceId)

err := cluster.Delete(client, instanceId)
_, err := cluster.Delete(client, instanceId, nil)
th.AssertNoErr(t, err)

t.Logf("DataArts instance deleted: %s", instanceId)
Expand Down
111 changes: 67 additions & 44 deletions openstack/dataarts/v1.1/cluster/Create.go
Original file line number Diff line number Diff line change
@@ -1,71 +1,104 @@
package cluster

import (
"net/http"

golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/internal/build"
"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
tag "github.com/opentelekomcloud/gophertelekomcloud/openstack/common/tags"
)

type CreateOpts struct {
Cluster Cluster `json:"cluster" required:"true"`
AutoRemind *bool `json:"auto_remind,omitempty"`
PhoneNum string `json:"phone_num,omitempty"`
Email string `json:"email,omitempty"`
Cluster Cluster `json:"cluster" required:"true"`
// AutoRemind Whether to enable message notification.
// If you enable this function, you can configure a maximum of five mobile numbers or email addresses.
// You will be notified of table/file migration job failures and EIP exceptions by SMS message or email.
AutoRemind bool `json:"auto_remind,omitempty"`
// PhoneNum Mobile number for receiving notifications.
PhoneNum string `json:"phone_num,omitempty"`
// Email address for receiving notifications.
Email string `json:"email,omitempty"`
}

type Cluster struct {
ScheduleBootTime string `json:"scheduleBootTime,omitempty"`
IsScheduleBootOff *bool `json:"isScheduleBootOff,omitempty"`
Instances []Instance `json:"instances,omitempty"`
DataStore *Datastore `json:"datastore,omitempty"`
ExtendedProperties *ExtendedProp `json:"extended_properties,omitempty"`
ScheduleOffTime string `json:"scheduleOffTime,omitempty"`
VpcId string `json:"vpcId,omitempty"`
Name string `json:"name,omitempty"`
SysTags []tag.ResourceTag `json:"sys_tags,omitempty"`
IsAutoOff *bool `json:"isAutoOff"`
// ScheduleBootTime Time for scheduled startup of a CDM cluster. The CDM cluster starts at this time every day.
ScheduleBootTime string `json:"scheduleBootTime,omitempty"`
// IsScheduleBootOff Whether to enable scheduled startup/shutdown. The scheduled startup/shutdown and auto shutdown functions cannot be enabled at the same time.
IsScheduleBootOff *bool `json:"isScheduleBootOff,omitempty"`
// Instances Node list.
Instances []Instance `json:"instances,omitempty"`
// DataStore Cluster information.
DataStore *Datastore `json:"datastore,omitempty"`
// ExtendedProperties Extended attribute.
ExtendedProperties *ExtendedProp `json:"extended_properties,omitempty"`
// ScheduleOffTime Time for scheduled shutdown of a CDM cluster. The CDM cluster shuts down directly at this time every day without waiting for unfinished jobs to complete.
ScheduleOffTime string `json:"scheduleOffTime,omitempty"`
// VpcId VPC ID, which is used for configuring a network for the cluster.
VpcId string `json:"vpcId,omitempty"`
// Name Cluster name.
Name string `json:"name,omitempty"`
// SysTags Enterprise project information. For details, see the descriptions of sys_tags parameters.
SysTags []tag.ResourceTag `json:"sys_tags,omitempty"`
// IsAutoOff Whether to enable auto shutdown. The auto shutdown and scheduled startup/shutdown functions cannot be enabled at the same time.
// When auto shutdown is enabled, if no job is running in the cluster and no scheduled job is available, a cluster will be automatically shut down 15 minutes after it starts running, which reduces costs for you.
IsAutoOff bool `json:"isAutoOff,omitempty"`
}

type Instance struct {
AZ string `json:"availability_zone" required:"true"`
Nics []Nic `json:"nics" required:"true"`
// AZ availability zone where a cluster is located.
AZ string `json:"availability_zone" required:"true"`
// Nics NIC list. A maximum of two NICs are supported. For details, see the descriptions of nics parameters.
Nics []Nic `json:"nics" required:"true"`
// FlavorRef Instance flavor.
FlavorRef string `json:"flavorRef" required:"true"`
Type string `json:"type" required:"true"`
// Type Node type. Currently, only cdm is available.
Type string `json:"type" required:"true"`
}

type Nic struct {
// SecurityGroupId Security group ID.
SecurityGroupId string `json:"securityGroupId" required:"true"`
// NetId Subnet ID.
NetId string `json:"net-id" required:"true"`
}

type Datastore struct {
Type string `json:"type,omitempty"`
// Type Generally, the value is cdm.
Type string `json:"type,omitempty"`
// Version Cluster version.
Version string `json:"version,omitempty"`
}

type ExtendedProp struct {
// WorkSpaceId Workspace ID.
WorkSpaceId string `json:"workSpaceId,omitempty"`
ResourceId string `json:"resourceId,omitempty"`
Trial string `json:"trial,omitempty"`
// ResourceId Resource ID.
ResourceId string `json:"resourceId,omitempty"`
// Trial Whether the cluster is a trial cluster.
Trial string `json:"trial,omitempty"`
}

type Nic struct {
SecurityGroupId string `json:"securityGroupId" required:"true"`
NetId string `json:"net-id" required:"true"`
}

func Create(client *golangsdk.ServiceClient, opts CreateOpts) (*ClusterResponse, error) {
// Create is used to create a cluster.
// Send request POST /v1.1/{project_id}/clusters
func Create(client *golangsdk.ServiceClient, opts CreateOpts, xLang string) (*ClusterResp, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe better to add xLang to CreateOpts as:

Xlang           string            `json:"-"`

b, err := build.RequestBody(opts, "")
if err != nil {
return nil, err
}

// POST /v1.1/{project_id}/clusters
raw, err := client.Post(client.ServiceURL("clusters"), b, nil, &golangsdk.RequestOpts{
MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en"},
raw, err := client.Post(client.ServiceURL(clustersEndpoint), b, nil, &golangsdk.RequestOpts{
MoreHeaders: map[string]string{HeaderContentType: ApplicationJson, HeaderXLanguage: xLang},
})
return extra(err, raw)

if err != nil {
return nil, err
}

var res *ClusterResp
err = extract.Into(raw.Body, res)
return res, err
}

type ClusterResponse struct {
type ClusterResp struct {
Name string `json:"name"`
Id string `json:"id"`
Task Task `json:"task"`
Expand All @@ -79,18 +112,8 @@ type Task struct {
}

type InstanceResp struct {
Name string `json:"name"`
Id string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
ShardId string `json:"shard_id"`
}

func extra(err error, raw *http.Response) (*ClusterResponse, error) {
if err != nil {
return nil, err
}

var res ClusterResponse
err = extract.Into(raw.Body, &res)
return &res, err
}
33 changes: 26 additions & 7 deletions openstack/dataarts/v1.1/cluster/Delete.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
package cluster

import "github.com/opentelekomcloud/gophertelekomcloud"
import (
"github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/internal/build"
"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
)

func Delete(client *golangsdk.ServiceClient, id string) (err error) {
// DELETE /v1.1/{project_id}/clusters/{cluster_id}
type KeepBackup struct {
KeepBackup int `json:"keep_last_manual_backup"`
type DeleteOpts struct {
// KeepBackup Number of backup log files. Retain the default value 0.
KeepBackup int `json:"keep_last_manual_backup"`
}

// Delete is used to delete a cluster.
// Send request DELETE /v1.1/{project_id}/clusters/{cluster_id}
func Delete(client *golangsdk.ServiceClient, id string, jsonOpts *DeleteOpts) (*JobId, error) {
b, err := build.RequestBody(jsonOpts, "")
if err != nil {
return nil, err
}

r, err := client.DeleteWithBody(client.ServiceURL(clustersEndpoint, id), b, nil)
if err != nil {
return nil, err
}
_, err = client.DeleteWithBody(client.ServiceURL("clusters", id), KeepBackup{KeepBackup: 0}, nil)
return

var resp *JobId
err = extract.Into(r.Body, resp)

return resp, err
}
27 changes: 11 additions & 16 deletions openstack/dataarts/v1.1/cluster/Get.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
package cluster

import (
"net/http"

golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
)

// Get is used to query cluster details.
// Send request GET /v1.1/{project_id}/clusters/{cluster_id}
func Get(client *golangsdk.ServiceClient, clusterId string) (*ClusterQuery, error) {
// GET /v1.1/{project_id}/clusters/{cluster_id}
raw, err := client.Get(client.ServiceURL("clusters", clusterId), nil, nil)
return extraResp(err, raw)
}

func extraResp(err error, raw *http.Response) (*ClusterQuery, error) {
raw, err := client.Get(client.ServiceURL(clustersEndpoint, clusterId), nil, nil)
if err != nil {
return nil, err
}

var res ClusterQuery
err = extract.Into(raw.Body, &res)
return &res, err
var res *ClusterQuery
err = extract.Into(raw.Body, res)
return res, err
}

type ClusterQuery struct {
PublicEndpoint string `json:"public_endpoint"`
PublicEndpoint string `json:"publicEndpoint"`
Instances []DetailedInstances `json:"instances"`
SecurityGroupId string `json:"security_group_id"`
SubnetId string `json:"subnet_id"`
Expand Down Expand Up @@ -66,9 +61,9 @@ type DetailedInstances struct {
Volume Volume `json:"volume"`
Status string `json:"status"`
Actions []string `json:"actions"`
Type string `json:"string"`
Name string `json:"name"`
Type string `json:"type"`
Id string `json:"id"`
Name string `json:"name"`
IsFrozen string `json:"isFrozen"`
Components string `json:"components"`
ConfigStatus string `json:"config_status"`
Expand Down Expand Up @@ -115,7 +110,7 @@ type CustomerConfig struct {
}

type MaintainWindow struct {
Dat string `json:"day"`
Day string `json:"day"`
StartTime string `json:"startTime"`
EndTime string `json:"endTime"`
}
Expand All @@ -130,8 +125,8 @@ type FailedReasons struct {
}

type CreateFailed struct {
ErrorMsg string `json:"errorMsg"`
ErrorCode string `json:"errorCode"`
ErrorMsg string `json:"errorMsg"`
}

type ClusterLinks struct {
Expand Down
20 changes: 20 additions & 0 deletions openstack/dataarts/v1.1/cluster/List.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cluster

import (
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
)

// List Querying the Cluster List.
// Send request GET /v1.1/{project_id}/clusters
func List(client *golangsdk.ServiceClient) ([]*ClusterQuery, error) {

raw, err := client.Get(client.ServiceURL(clustersEndpoint), nil, nil)
if err != nil {
return nil, err
}

var res []*ClusterQuery
err = extract.IntoSlicePtr(raw.Body, &res, "clusters")
return res, err
}
54 changes: 42 additions & 12 deletions openstack/dataarts/v1.1/cluster/Restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,58 @@ import (
"github.com/opentelekomcloud/gophertelekomcloud/internal/build"
)

const (
RestartImmediately = "IMMEDIATELY"
RestartForcely = "FORCELY"
RestartSoftly = "SOFTLY"
)

type RestartOpts struct {
Id string `json:"-"`
// Restart Cluster restart. For details about how to define the cluster to restart, see RestartStruct.
Restart RestartStruct `json:"restart" required:"true"`
}

type RestartStruct struct {
StopMode int `json:"restartDelayTime,omitempty"`
RestartMode string `json:"restartMode,omitempty"`
// RestartDelayTime Restart delay, in seconds.
RestartDelayTime int `json:"restartDelayTime,omitempty"`
// Restart mode
// IMMEDIATELY: immediate restart
// FORCELY: forcible restart
// SOFTLY: common restart
// The default value is IMMEDIATELY. Forcibly restarting the service process will interrupt the service process and restart the VMs in the cluster.
RestartMode string `json:"restartMode,omitempty"`
// RestartLevel Restart level
// SERVICE: service restart
// VM: VM restart
// The default value is SERVICE.
RestartLevel string `json:"restartLevel,omitempty"`
Type string `json:"type,omitempty"`
Instance string `json:"instance,omitempty"`
Group string `json:"group,omitempty"`
// Type Type of the cluster to be restarted. The value can be set to cdm only.
Type string `json:"type,omitempty"`
// Instance Reserved field. When restartLevel is set to SERVICE, this parameter is mandatory and an empty string should be entered.
Instance string `json:"instance,omitempty"`
// Reserved field. When restartLevel is set to SERVICE, this parameter is mandatory and an empty string should be entered.
Group string `json:"group,omitempty"`
}

func Restart(client *golangsdk.ServiceClient, opts RestartOpts) (*JobId, error) {
// Restart is used to restart a cluster.
// Send request POST /v1.1/{project_id}/clusters/{cluster_id}/action
func Restart(client *golangsdk.ServiceClient, clusterId string, opts RestartOpts) (*JobId, error) {
b, err := build.RequestBody(opts, "")
if err != nil {
return nil, err
}
// POST /v1.1/{project_id}/clusters/{cluster_id}/action
raw, err := client.Post(client.ServiceURL("clusters", opts.Id, "action"), b, nil, &golangsdk.RequestOpts{
MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en"},
})
return extraJob(err, raw)

resp, err := client.Post(
client.ServiceURL(clustersEndpoint, clusterId, actionEndpoint),
b,
nil,
&golangsdk.RequestOpts{
MoreHeaders: map[string]string{HeaderContentType: ApplicationJson},
},
)
if err != nil {
return nil, err
}

return respToJobId(resp)
}
Loading