diff --git a/openstack/client.go b/openstack/client.go index 9d0494832..d5e8026c8 100644 --- a/openstack/client.go +++ b/openstack/client.go @@ -561,6 +561,21 @@ func initClientOpts(client *golangsdk.ProviderClient, eo golangsdk.EndpointOpts, return sc, nil } +// initcommonServiceClient create a ServiceClient which can not get from clientType directly. +// firstly, we initialize a service client by "volumev2" type, the endpoint likes https://evs.{region}.{xxx.com}/v2/{project_id} +// then we replace the endpoint with the specified srv and version. +func initcommonServiceClient(client *golangsdk.ProviderClient, eo golangsdk.EndpointOpts, srv string, version string) (*golangsdk.ServiceClient, error) { + sc, err := initClientOpts(client, eo, "volumev2") + if err != nil { + return nil, err + } + + e := strings.Replace(sc.Endpoint, "v2", version, 1) + sc.Endpoint = strings.Replace(e, "evs", srv, 1) + sc.ResourceBase = sc.Endpoint + return sc, err +} + // NewObjectStorageV1 creates a ServiceClient that may be used with the v1 // object storage package. func NewObjectStorageV1(client *golangsdk.ProviderClient, eo golangsdk.EndpointOpts) (*golangsdk.ServiceClient, error) { @@ -1039,3 +1054,9 @@ func NewDDSV3(client *golangsdk.ProviderClient, eo golangsdk.EndpointOpts) (*gol sc, err := initClientOpts(client, eo, "ddsv3") return sc, err } + +// NewLTSV2 creates a ServiceClient that may be used to access the LTS service. +func NewLTSV2(client *golangsdk.ProviderClient, eo golangsdk.EndpointOpts) (*golangsdk.ServiceClient, error) { + sc, err := initcommonServiceClient(client, eo, "lts", "v2.0") + return sc, err +} diff --git a/openstack/lts/v2/loggroups/requests.go b/openstack/lts/v2/loggroups/requests.go new file mode 100644 index 000000000..77e35040a --- /dev/null +++ b/openstack/lts/v2/loggroups/requests.go @@ -0,0 +1,49 @@ +package loggroups + +import "github.com/huaweicloud/golangsdk" + +// CreateOptsBuilder is used for creating log group parameters. +type CreateOptsBuilder interface { + ToLogGroupsCreateMap() (map[string]interface{}, error) +} + +// CreateOpts is a struct that contains all the parameters. +type CreateOpts struct { + // Specifies the log group name. + LogGroupName string `json:"log_group_name" required:"true"` + + // Specifies the log expiration time. The value is fixed to 7 days. + TTL int `json:"ttl_in_daysion,omitempty"` +} + +// ToLogGroupsCreateMap is used for type convert +func (ops CreateOpts) ToLogGroupsCreateMap() (map[string]interface{}, error) { + return golangsdk.BuildRequestBody(ops, "") +} + +// Create a log group with given parameters. +func Create(client *golangsdk.ServiceClient, ops CreateOptsBuilder) (r CreateResult) { + b, err := ops.ToLogGroupsCreateMap() + if err != nil { + r.Err = err + return + } + + _, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{ + OkCodes: []int{201}, + }) + + return +} + +// Delete a log group by id +func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) { + _, r.Err = client.Delete(deleteURL(client, id), nil) + return +} + +// Get a log group with detailed information by id +func Get(client *golangsdk.ServiceClient, id string) (r GetResult) { + _, r.Err = client.Get(getURL(client, id), &r.Body, nil) + return +} diff --git a/openstack/lts/v2/loggroups/results.go b/openstack/lts/v2/loggroups/results.go new file mode 100644 index 000000000..893e104a9 --- /dev/null +++ b/openstack/lts/v2/loggroups/results.go @@ -0,0 +1,45 @@ +package loggroups + +import "github.com/huaweicloud/golangsdk" + +// Log group Create response +type CreateResponse struct { + ID string `json:"log_group_id"` +} + +// CreateResult is a struct that contains all the return parameters of creation +type CreateResult struct { + golangsdk.Result +} + +// Extract from CreateResult +func (r CreateResult) Extract() (*CreateResponse, error) { + s := new(CreateResponse) + err := r.Result.ExtractInto(s) + return s, err +} + +// DeleteResult is a struct which contains the result of deletion +type DeleteResult struct { + golangsdk.ErrResult +} + +// Log group response +type LogGroup struct { + ID string `json:"log_group_id"` + Name string `json:"log_group_name"` + CreationTime int64 `json:"creation_time"` + TTLinDays int `json:"ttl_in_days"` +} + +// GetResult contains the body of getting detailed +type GetResult struct { + golangsdk.Result +} + +// Extract from GetResult +func (r GetResult) Extract() (*LogGroup, error) { + s := new(LogGroup) + err := r.Result.ExtractInto(s) + return s, err +} diff --git a/openstack/lts/v2/loggroups/urls.go b/openstack/lts/v2/loggroups/urls.go new file mode 100644 index 000000000..ead5443f1 --- /dev/null +++ b/openstack/lts/v2/loggroups/urls.go @@ -0,0 +1,20 @@ +package loggroups + +import "github.com/huaweicloud/golangsdk" + +const rootPath = "log-groups" + +// createURL will build the url of creation +func createURL(client *golangsdk.ServiceClient) string { + return client.ServiceURL(rootPath) +} + +// deleteURL will build the url of deletion +func deleteURL(client *golangsdk.ServiceClient, id string) string { + return client.ServiceURL(rootPath, id) +} + +// getURL will build the get url of get function +func getURL(client *golangsdk.ServiceClient, id string) string { + return client.ServiceURL(rootPath, id) +} diff --git a/openstack/lts/v2/logtopics/requests.go b/openstack/lts/v2/logtopics/requests.go new file mode 100644 index 000000000..a1cd5a567 --- /dev/null +++ b/openstack/lts/v2/logtopics/requests.go @@ -0,0 +1,46 @@ +package logtopics + +import "github.com/huaweicloud/golangsdk" + +// CreateOptsBuilder is used for creating log group parameters. +type CreateOptsBuilder interface { + ToLogTopicsCreateMap() (map[string]interface{}, error) +} + +// CreateOpts is a struct that contains all the parameters. +type CreateOpts struct { + // Specifies the log group name. + LogTopicName string `json:"log_topic_name" required:"true"` +} + +// ToLogTopicsCreateMap is used for type convert +func (ops CreateOpts) ToLogTopicsCreateMap() (map[string]interface{}, error) { + return golangsdk.BuildRequestBody(ops, "") +} + +// Create a log topic with given parameters. +func Create(client *golangsdk.ServiceClient, groupId string, ops CreateOptsBuilder) (r CreateResult) { + b, err := ops.ToLogTopicsCreateMap() + if err != nil { + r.Err = err + return + } + + _, r.Err = client.Post(createURL(client, groupId), b, &r.Body, &golangsdk.RequestOpts{ + OkCodes: []int{201}, + }) + + return +} + +// Delete a log topic by id +func Delete(client *golangsdk.ServiceClient, groupId string, id string) (r DeleteResult) { + _, r.Err = client.Delete(deleteURL(client, groupId, id), nil) + return +} + +// Get a log topic with detailed information by id +func Get(client *golangsdk.ServiceClient, groupId string, id string) (r GetResult) { + _, r.Err = client.Get(getURL(client, groupId, id), &r.Body, nil) + return +} diff --git a/openstack/lts/v2/logtopics/results.go b/openstack/lts/v2/logtopics/results.go new file mode 100644 index 000000000..83003f39f --- /dev/null +++ b/openstack/lts/v2/logtopics/results.go @@ -0,0 +1,45 @@ +package logtopics + +import "github.com/huaweicloud/golangsdk" + +// Log topic Create response +type CreateResponse struct { + ID string `json:"log_topic_id"` +} + +// CreateResult is a struct that contains all the return parameters of creation +type CreateResult struct { + golangsdk.Result +} + +// Extract from CreateResult +func (r CreateResult) Extract() (*CreateResponse, error) { + s := new(CreateResponse) + err := r.Result.ExtractInto(s) + return s, err +} + +// DeleteResult is a struct which contains the result of deletion +type DeleteResult struct { + golangsdk.ErrResult +} + +// Log topic response +type LogTopic struct { + ID string `json:"log_topic_id,omitempty"` + Name string `json:"log_topic_name"` + CreationTime int64 `json:"creation_time"` + IndexEnabled bool `json:"index_enabled"` +} + +// GetResult contains the body of getting detailed +type GetResult struct { + golangsdk.Result +} + +// Extract from GetResult +func (r GetResult) Extract() (*LogTopic, error) { + s := new(LogTopic) + err := r.Result.ExtractInto(s) + return s, err +} diff --git a/openstack/lts/v2/logtopics/urls.go b/openstack/lts/v2/logtopics/urls.go new file mode 100644 index 000000000..dabaca408 --- /dev/null +++ b/openstack/lts/v2/logtopics/urls.go @@ -0,0 +1,23 @@ +package logtopics + +import "github.com/huaweicloud/golangsdk" + +const ( + resourcePath = "log-topics" + rootPath = "log-groups" +) + +// createURL will build the url of creation +func createURL(client *golangsdk.ServiceClient, groupId string) string { + return client.ServiceURL(rootPath, groupId, resourcePath) +} + +// deleteURL will build the url of deletion +func deleteURL(client *golangsdk.ServiceClient, groupId string, id string) string { + return client.ServiceURL(rootPath, groupId, resourcePath, id) +} + +// getURL will build the get url of get function +func getURL(client *golangsdk.ServiceClient, groupId string, id string) string { + return client.ServiceURL(rootPath, groupId, resourcePath, id) +}