Skip to content

Commit

Permalink
[LTS] API Update (#469)
Browse files Browse the repository at this point in the history
[LTS] API Update

fix #428

Reviewed-by: Artem Lifshits
Reviewed-by: Anton Sidelnikov
Reviewed-by: Aloento
  • Loading branch information
Aloento committed Jan 10, 2023
1 parent 826e899 commit 54b5597
Show file tree
Hide file tree
Showing 21 changed files with 851 additions and 270 deletions.
37 changes: 0 additions & 37 deletions acceptance/openstack/lts/v2/loggroups_test.go

This file was deleted.

144 changes: 144 additions & 0 deletions acceptance/openstack/lts/v2/lts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package v2

import (
"strings"
"testing"

"github.com/opentelekomcloud/gophertelekomcloud/acceptance/clients"
"github.com/opentelekomcloud/gophertelekomcloud/acceptance/tools"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/lts/v2/groups"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/lts/v2/streams"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/lts/v2/transfers"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/obs"
th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
)

func TestLtsLifecycle(t *testing.T) {
client, err := clients.NewLtsV2Client()
th.AssertNoErr(t, err)

name := tools.RandomString("test-group-", 3)
createOpts := groups.CreateOpts{
LogGroupName: name,
TTLInDays: 7,
}

created, err := groups.CreateLogGroup(client, createOpts)
th.AssertNoErr(t, err)

t.Cleanup(func() {
err = groups.DeleteLogGroup(client, created)
th.AssertNoErr(t, err)
})

group, err := groups.UpdateLogGroup(client, groups.UpdateLogGroupOpts{
LogGroupId: created,
TTLInDays: 3,
})
th.AssertNoErr(t, err)
th.AssertEquals(t, 3, group.TTLInDays)

got, err := groups.ListLogGroups(client)
th.AssertNoErr(t, err)
th.AssertEquals(t, true, len(got) > 0)
tools.PrintResource(t, got)

sname := tools.RandomString("test-stream-", 3)
stream, err := streams.CreateLogStream(client, streams.CreateOpts{
GroupId: created,
LogStreamName: sname,
})
th.AssertNoErr(t, err)

t.Cleanup(func() {
err = streams.DeleteLogStream(client, streams.DeleteOpts{
GroupId: created,
StreamId: stream,
})
th.AssertNoErr(t, err)
})

slist, err := streams.ListLogStream(client, created)
th.AssertNoErr(t, err)
th.AssertEquals(t, true, len(slist) > 0)
tools.PrintResource(t, slist)
}

func TestLtsTransferLifecycle(t *testing.T) {
t.Skipf("Released API are not working properly")
client, err := clients.NewLtsV2Client()
th.AssertNoErr(t, err)

name := tools.RandomString("test-group-", 3)
createOpts := groups.CreateOpts{
LogGroupName: name,
TTLInDays: 7,
}

logId, err := groups.CreateLogGroup(client, createOpts)
th.AssertNoErr(t, err)

t.Cleanup(func() {
err = groups.DeleteLogGroup(client, logId)
th.AssertNoErr(t, err)
})

sname := tools.RandomString("test-stream-", 3)
streamId, err := streams.CreateLogStream(client, streams.CreateOpts{
GroupId: logId,
LogStreamName: sname,
})
th.AssertNoErr(t, err)

t.Cleanup(func() {
err = streams.DeleteLogStream(client, streams.DeleteOpts{
GroupId: logId,
StreamId: streamId,
})
th.AssertNoErr(t, err)
})

obsClient, err := clients.NewOBSClient()
th.AssertNoErr(t, err)

bucketName := strings.ToLower(tools.RandomString("obs-test", 5))

_, err = obsClient.CreateBucket(&obs.CreateBucketInput{
Bucket: bucketName,
})
t.Cleanup(func() {
_, err = obsClient.DeleteBucket(bucketName)
th.AssertNoErr(t, err)
})
th.AssertNoErr(t, err)

switchOn := false
createTransferOpts := transfers.CreateLogDumpObsOpts{
LogGroupId: logId,
LogStreamIds: []string{
streamId,
},
ObsBucketName: bucketName,
Type: "cycle",
StorageFormat: "RAW",
SwitchOn: &switchOn,
PrefixName: "test",
DirPrefixName: "dir-test",
Period: 3,
PeriodUnit: "hour",
}
logDumpId, err := transfers.CreateLogDumpObs(client, createTransferOpts)
th.AssertNoErr(t, err)
t.Logf("Obs log dump created, id: %s", logDumpId)

// GET API is currently not working

listLogs, err := transfers.ListTransfers(client, transfers.ListTransfersOpts{})
th.AssertNoErr(t, err)
if len(listLogs) < 1 {
t.Error("Log dump wasn't found")
}

err = transfers.DeleteTransfer(client, logDumpId)
th.AssertNoErr(t, err)
}
2 changes: 1 addition & 1 deletion openstack/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ func NewSDRSV1(client *golangsdk.ProviderClient, eo golangsdk.EndpointOpts) (*go

// 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")
sc, err := initCommonServiceClient(client, eo, "lts", "v2")
return sc, err
}

Expand Down
38 changes: 38 additions & 0 deletions openstack/lts/v2/groups/CreateLogGroup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package groups

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

// CreateOpts is a struct that contains all the parameters.
type CreateOpts struct {
// Name of the log group to be created.
// Minimum length: 1 character
// Maximum length: 64 characters
// Enumerated value:
// lts-group-01nh
LogGroupName string `json:"log_group_name" required:"true"`
// Log retention duration, in days (fixed to 7 days).
TTLInDays int `json:"ttl_in_days"`
}

func CreateLogGroup(client *golangsdk.ServiceClient, opts CreateOpts) (string, error) {
b, err := build.RequestBody(opts, "")
if err != nil {
return "", err
}

// POST /v2/{project_id}/groups
raw, err := client.Post(client.ServiceURL("groups"), b, nil, nil)
if err != nil {
return "", err
}

var res struct {
ID string `json:"log_group_id"`
}
err = extract.Into(raw.Body, &res)
return res.ID, err
}
14 changes: 14 additions & 0 deletions openstack/lts/v2/groups/DeleteLogGroup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package groups

import "github.com/opentelekomcloud/gophertelekomcloud"

// DeleteLogGroup a log group by id
func DeleteLogGroup(client *golangsdk.ServiceClient, groupId string) (err error) {
// DELETE /v2/{project_id}/groups/{log_group_id}
_, err = client.Delete(client.ServiceURL("groups", groupId), &golangsdk.RequestOpts{
MoreHeaders: map[string]string{
"content-type": "application/json",
},
})
return
}
38 changes: 38 additions & 0 deletions openstack/lts/v2/groups/ListLogGroups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package groups

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

func ListLogGroups(client *golangsdk.ServiceClient) ([]LogGroup, error) {
// GET /v2/{project_id}/groups
raw, err := client.Get(client.ServiceURL("groups"), nil, &golangsdk.RequestOpts{
MoreHeaders: map[string]string{
"content-type": "application/json",
},
})
if err != nil {
return nil, err
}

var res []LogGroup
err = extract.IntoSlicePtr(raw.Body, &res, "log_groups")
return res, err
}

type LogGroup struct {
// Time when a log group was created.
CreationTime int64 `json:"creation_time"`
// Log group name.
// Minimum length: 1 character
// Maximum length: 64 characters
LogGroupName string `json:"log_group_name"`
// Log group ID.
// Value length: 36 characters
LogGroupId string `json:"log_group_id"`
// Log retention duration, in days (fixed to 7 days).
TTLInDays int `json:"ttl_in_days"`
// Log group tag.
Tag map[string]string `json:"tag,omitempty"`
}
35 changes: 35 additions & 0 deletions openstack/lts/v2/groups/UpdateLogGroup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package groups

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

type UpdateLogGroupOpts struct {
// Log group ID. For details about how to obtain a log group ID, see Obtaining the AccountID, Project ID, Log Group ID, and Log Stream ID.
// Default value: None
// Value length: 36 characters
LogGroupId string `json:"-" required:"true"`
// Log retention duration, in days (fixed to 7 days).
TTLInDays int32 `json:"ttl_in_days" required:"true"`
}

func UpdateLogGroup(client *golangsdk.ServiceClient, opts UpdateLogGroupOpts) (*LogGroup, error) {
b, err := build.RequestBody(opts, "")
if err != nil {
return nil, err
}

// POST /v2/{project_id}/groups/{log_group_id}
raw, err := client.Post(client.ServiceURL("groups", opts.LogGroupId), b, nil, &golangsdk.RequestOpts{
OkCodes: []int{200},
})
if err != nil {
return nil, err
}

var res LogGroup
err = extract.Into(raw.Body, &res)
return &res, err
}
53 changes: 0 additions & 53 deletions openstack/lts/v2/loggroups/requests.go

This file was deleted.

Loading

0 comments on commit 54b5597

Please sign in to comment.