2019-10-12 04:46:21 -04:00
|
|
|
package common
|
2019-06-13 03:16:49 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ucloud/ucloud-sdk-go/services/uaccount"
|
2019-10-12 04:46:21 -04:00
|
|
|
"github.com/ucloud/ucloud-sdk-go/services/ufile"
|
2019-06-13 03:16:49 -04:00
|
|
|
"github.com/ucloud/ucloud-sdk-go/services/uhost"
|
|
|
|
"github.com/ucloud/ucloud-sdk-go/services/unet"
|
|
|
|
"github.com/ucloud/ucloud-sdk-go/services/vpc"
|
|
|
|
"github.com/ucloud/ucloud-sdk-go/ucloud"
|
2019-12-17 05:25:56 -05:00
|
|
|
uerr "github.com/ucloud/ucloud-sdk-go/ucloud/error"
|
2019-06-13 03:16:49 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type UCloudClient struct {
|
2019-10-12 04:46:21 -04:00
|
|
|
UHostConn *uhost.UHostClient
|
|
|
|
UNetConn *unet.UNetClient
|
|
|
|
VPCConn *vpc.VPCClient
|
|
|
|
UAccountConn *uaccount.UAccountClient
|
|
|
|
UFileConn *ufile.UFileClient
|
2019-06-13 03:16:49 -04:00
|
|
|
}
|
|
|
|
|
2019-10-12 04:46:21 -04:00
|
|
|
func (c *UCloudClient) DescribeFirewallById(sgId string) (*unet.FirewallDataSet, error) {
|
2019-06-13 03:16:49 -04:00
|
|
|
if sgId == "" {
|
2019-10-12 04:46:21 -04:00
|
|
|
return nil, NewNotFoundError("security group", sgId)
|
2019-06-13 03:16:49 -04:00
|
|
|
}
|
2019-10-12 04:46:21 -04:00
|
|
|
conn := c.UNetConn
|
2019-06-13 03:16:49 -04:00
|
|
|
|
|
|
|
req := conn.NewDescribeFirewallRequest()
|
|
|
|
req.FWId = ucloud.String(sgId)
|
|
|
|
|
|
|
|
resp, err := conn.DescribeFirewall(req)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if uErr, ok := err.(uerr.Error); ok && uErr.Code() == 54002 {
|
2019-10-12 04:46:21 -04:00
|
|
|
return nil, NewNotFoundError("security group", sgId)
|
2019-06-13 03:16:49 -04:00
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.DataSet) < 1 {
|
2019-10-12 04:46:21 -04:00
|
|
|
return nil, NewNotFoundError("security group", sgId)
|
2019-06-13 03:16:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return &resp.DataSet[0], nil
|
|
|
|
}
|
|
|
|
|
2019-10-12 04:46:21 -04:00
|
|
|
func (c *UCloudClient) DescribeSubnetById(subnetId string) (*vpc.VPCSubnetInfoSet, error) {
|
2019-06-13 03:16:49 -04:00
|
|
|
if subnetId == "" {
|
2019-10-12 04:46:21 -04:00
|
|
|
return nil, NewNotFoundError("Subnet", subnetId)
|
2019-06-13 03:16:49 -04:00
|
|
|
}
|
2019-10-12 04:46:21 -04:00
|
|
|
conn := c.VPCConn
|
2019-06-13 03:16:49 -04:00
|
|
|
|
|
|
|
req := conn.NewDescribeSubnetRequest()
|
|
|
|
req.SubnetIds = []string{subnetId}
|
|
|
|
|
|
|
|
resp, err := conn.DescribeSubnet(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp == nil || len(resp.DataSet) < 1 {
|
2019-10-12 04:46:21 -04:00
|
|
|
return nil, NewNotFoundError("Subnet", subnetId)
|
2019-06-13 03:16:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return &resp.DataSet[0], nil
|
|
|
|
}
|
|
|
|
|
2019-10-12 04:46:21 -04:00
|
|
|
func (c *UCloudClient) DescribeVPCById(vpcId string) (*vpc.VPCInfo, error) {
|
2019-06-13 03:16:49 -04:00
|
|
|
if vpcId == "" {
|
2019-10-12 04:46:21 -04:00
|
|
|
return nil, NewNotFoundError("VPC", vpcId)
|
2019-06-13 03:16:49 -04:00
|
|
|
}
|
2019-10-12 04:46:21 -04:00
|
|
|
conn := c.VPCConn
|
2019-06-13 03:16:49 -04:00
|
|
|
|
|
|
|
req := conn.NewDescribeVPCRequest()
|
|
|
|
req.VPCIds = []string{vpcId}
|
|
|
|
|
|
|
|
resp, err := conn.DescribeVPC(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp == nil || len(resp.DataSet) < 1 {
|
2019-10-12 04:46:21 -04:00
|
|
|
return nil, NewNotFoundError("VPC", vpcId)
|
2019-06-13 03:16:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return &resp.DataSet[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *UCloudClient) DescribeImageById(imageId string) (*uhost.UHostImageSet, error) {
|
|
|
|
if imageId == "" {
|
2019-10-12 04:46:21 -04:00
|
|
|
return nil, NewNotFoundError("image", imageId)
|
2019-06-13 03:16:49 -04:00
|
|
|
}
|
2019-10-12 04:46:21 -04:00
|
|
|
req := c.UHostConn.NewDescribeImageRequest()
|
2019-06-13 03:16:49 -04:00
|
|
|
req.ImageId = ucloud.String(imageId)
|
|
|
|
|
2019-10-12 04:46:21 -04:00
|
|
|
resp, err := c.UHostConn.DescribeImage(req)
|
2019-06-13 03:16:49 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.ImageSet) < 1 {
|
2019-10-12 04:46:21 -04:00
|
|
|
return nil, NewNotFoundError("image", imageId)
|
2019-06-13 03:16:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return &resp.ImageSet[0], nil
|
|
|
|
}
|
|
|
|
|
2019-10-12 04:46:21 -04:00
|
|
|
func (c *UCloudClient) DescribeUHostById(uhostId string) (*uhost.UHostInstanceSet, error) {
|
2019-06-13 03:16:49 -04:00
|
|
|
if uhostId == "" {
|
2019-10-12 04:46:21 -04:00
|
|
|
return nil, NewNotFoundError("instance", uhostId)
|
2019-06-13 03:16:49 -04:00
|
|
|
}
|
2019-10-12 04:46:21 -04:00
|
|
|
req := c.UHostConn.NewDescribeUHostInstanceRequest()
|
2019-06-13 03:16:49 -04:00
|
|
|
req.UHostIds = []string{uhostId}
|
|
|
|
|
2019-10-12 04:46:21 -04:00
|
|
|
resp, err := c.UHostConn.DescribeUHostInstance(req)
|
2019-06-13 03:16:49 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(resp.UHostSet) < 1 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &resp.UHostSet[0], nil
|
|
|
|
}
|
|
|
|
|
2019-10-12 04:46:21 -04:00
|
|
|
func (c *UCloudClient) DescribeImageByInfo(projectId, regionId, imageId string) (*uhost.UHostImageSet, error) {
|
|
|
|
req := c.UHostConn.NewDescribeImageRequest()
|
2019-06-13 03:16:49 -04:00
|
|
|
req.ProjectId = ucloud.String(projectId)
|
|
|
|
req.ImageId = ucloud.String(imageId)
|
|
|
|
req.Region = ucloud.String(regionId)
|
|
|
|
|
2019-10-12 04:46:21 -04:00
|
|
|
resp, err := c.UHostConn.DescribeImage(req)
|
2019-06-13 03:16:49 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.ImageSet) < 1 {
|
2019-10-12 04:46:21 -04:00
|
|
|
return nil, NewNotFoundError("image", imageId)
|
2019-06-13 03:16:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return &resp.ImageSet[0], nil
|
|
|
|
|
|
|
|
}
|