Skip to content

Commit

Permalink
[Feat.] RMS query endpoints and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-sidelnikov committed Oct 15, 2024
1 parent 4af12d2 commit 8b82ce2
Show file tree
Hide file tree
Showing 17 changed files with 809 additions and 1 deletion.
11 changes: 11 additions & 0 deletions acceptance/clients/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,17 @@ func NewEVPNClient() (client *golangsdk.ServiceClient, err error) {
})
}

// NewRMSClient returns authenticated RMS v1 client
func NewRMSClient() (client *golangsdk.ServiceClient, err error) {
cc, err := CloudAndClient()
if err != nil {
return nil, err
}
return openstack.NewRmsServiceV1(cc.ProviderClient, golangsdk.EndpointOpts{
Region: cc.RegionName,
})
}

func UpdatePeerTenantDetails(cloud *openstack.Cloud) error {
if id := EnvOS.GetEnv("Peer_Tenant_ID"); id != "" {
cloud.AuthInfo.ProjectID = id
Expand Down
186 changes: 186 additions & 0 deletions acceptance/openstack/rms/v1/query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
package v1

import (
"testing"

"github.com/opentelekomcloud/gophertelekomcloud/acceptance/clients"
"github.com/opentelekomcloud/gophertelekomcloud/acceptance/tools"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/rms/query"
th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
)

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

listOpts := query.ListSpecificOpts{}
resources, err := query.ListSpecificType(
client,
client.DomainID,
"ecs",
"cloudservers",
listOpts,
)
th.AssertNoErr(t, err)
tools.AssertLengthGreaterThan(t, resources, 1)
}

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

listOpts := query.ListRecordedOpts{}
resources, err := query.ListRecordedResources(
client,
client.DomainID,
listOpts,
)
th.AssertNoErr(t, err)
th.AssertEquals(t, 0, len(resources))
}

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

listOpts := query.ListServicesOpts{}
resources, err := query.ListServices(
client,
client.DomainID,
listOpts,
)
th.AssertNoErr(t, err)
tools.AssertLengthGreaterThan(t, resources, 1)
}

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

listOpts := query.ListSpecificOpts{}
resources, err := query.ListSpecificType(
client,
client.DomainID,
"ecs",
"cloudservers",
listOpts,
)
th.AssertNoErr(t, err)
if len(resources) != 0 {
res, err := query.GetResource(
client,
client.DomainID,
"ecs",
"cloudservers",
resources[0].ID,
)
th.AssertNoErr(t, err)
th.AssertEquals(t, resources[0].ID, res.ID)
}
}

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

listOpts := query.ListRecordedTagsOpts{}
resources, err := query.ListRecordedResourcesTags(
client,
client.DomainID,
listOpts,
)
th.AssertNoErr(t, err)
th.AssertEquals(t, 0, len(resources))
}

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

listOpts := query.ListRecordedSummaryOpts{}
resources, err := query.ListRecordedResourcesSummary(
client,
client.DomainID,
listOpts,
)
th.AssertNoErr(t, err)
th.AssertEquals(t, 0, len(resources))
}

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

listOpts := query.ListAllOpts{}
resources, err := query.ListAllResources(
client,
client.DomainID,
listOpts,
)
th.AssertNoErr(t, err)
tools.AssertLengthGreaterThan(t, resources, 1)
}

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

listOpts := query.ListAllOpts{}
resources, err := query.ListAllResources(
client,
client.DomainID,
listOpts,
)
th.AssertNoErr(t, err)
if len(resources) != 0 {
res, err := query.GetAnyResource(
client,
client.DomainID,
resources[0].ID,
)
th.AssertNoErr(t, err)
th.AssertEquals(t, resources[0].ID, res.ID)
}
}

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

listOpts := query.ListAllTagsOpts{}
tags, err := query.ListAllResourcesTags(
client,
client.DomainID,
listOpts,
)
th.AssertNoErr(t, err)
tools.AssertLengthGreaterThan(t, tags, 1)
}

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

listOpts := query.CountOpts{}
count, err := query.GetCount(
client,
client.DomainID,
listOpts,
)
th.AssertNoErr(t, err)
tools.PrintResource(t, count)
}

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

listOpts := query.ListSummaryOpts{}
resources, err := query.ListResourcesSummary(
client,
client.DomainID,
listOpts,
)
th.AssertNoErr(t, err)
tools.AssertLengthGreaterThan(t, resources, 1)
}
28 changes: 28 additions & 0 deletions acceptance/tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
mrand "math/rand"
"reflect"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -97,3 +98,30 @@ func SetLastOctet(ip string, newLastOctet int) string {
// Reassemble the IP address
return strings.Join(parts, ".")
}

// logFatal is a helper function to log fatal errors during the test.
func logFatal(t *testing.T, message string) {
t.Helper()
t.Fatal(message)
}

// AssertLengthGreaterThan checks if the length of the provided list is greater than the specified number.
// If the condition fails, it logs a fatal error and fails the test.
func AssertLengthGreaterThan(t *testing.T, list interface{}, threshold int) {
t.Helper()

// Use reflection to get the length of the list
listValue := reflect.ValueOf(list)

// Ensure the list is a slice or array
if listValue.Kind() != reflect.Slice && listValue.Kind() != reflect.Array {
logFatal(t, fmt.Sprintf("expected a slice or array, but got %s", listValue.Kind().String()))
return
}

// Check if the length is greater than the threshold
listLen := listValue.Len()
if listLen <= threshold {
logFatal(t, fmt.Sprintf("expected length to be greater than %d, but got %d", threshold, listLen))
}
}
4 changes: 4 additions & 0 deletions openstack/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1002,3 +1002,7 @@ func NewEVPNServiceV3(client *golangsdk.ProviderClient, eo golangsdk.EndpointOpt
}
return sc, nil
}

func NewRmsServiceV1(client *golangsdk.ProviderClient, eo golangsdk.EndpointOpts) (*golangsdk.ServiceClient, error) {
return initClientOpts(client, eo, "rms")
}
3 changes: 2 additions & 1 deletion openstack/css/v1/clusters/Create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package clusters

import (
"github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/internal/build"
"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/common/tags"
)
Expand Down Expand Up @@ -94,7 +95,7 @@ type DiskEncryption struct {
}

func Create(client *golangsdk.ServiceClient, opts CreateOpts) (*CreatedCluster, error) {
b, err := golangsdk.BuildRequestBody(opts, "cluster")
b, err := build.RequestBody(opts, "cluster")
if err != nil {
return nil, err
}
Expand Down
20 changes: 20 additions & 0 deletions openstack/rms/query/GetAnyResource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package query

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

func GetAnyResource(client *golangsdk.ServiceClient, domainId, id string) (*Resource, error) {
// GET /v1/resource-manager/domains/{domain_id}/all-resources/{resource_id}
raw, err := client.Get(client.ServiceURL(
"resource-manager", "domains", domainId,
"all-resources", id), nil, nil)
if err != nil {
return nil, err
}

var res Resource
err = extract.Into(raw.Body, &res)
return &res, err
}
42 changes: 42 additions & 0 deletions openstack/rms/query/GetCount.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package query

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

type CountOpts struct {
// Specifies the region ID.
RegionId string `q:"region_id"`
// Specifies the project ID.
ProjectId string `q:"project_id"`
// Specifies the resource type
Type string `q:"type"`
// Specifies the resource ID.
Id string `q:"id"`
// Specifies the resource name.
Name string `q:"name"`
// Specifies tags. The format is key or key=value.
Tags []string `q:"tags"`
}

func GetCount(client *golangsdk.ServiceClient, domainId string, opts CountOpts) (*int, error) {
// GET /v1/resource-manager/domains/{domain_id}/all-resources/count
url, err := golangsdk.NewURLBuilder().
WithEndpoints("resource-manager", "domains", domainId, "all-resources", "count").
WithQueryParams(&opts).Build()
if err != nil {
return nil, err
}

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

var res struct {
Count int `json:"total_count"`
}
err = extract.Into(raw.Body, &res)
return &res.Count, err
}
20 changes: 20 additions & 0 deletions openstack/rms/query/GetRecordedResource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package query

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

func GetRecordedResource(client *golangsdk.ServiceClient, domainId, id string) (*Resource, error) {
// GET /v1/resource-manager/domains/{domain_id}/tracked-resources/{resource_id}
raw, err := client.Get(client.ServiceURL(
"resource-manager", "domains", domainId,
"tracked-resources", id), nil, nil)
if err != nil {
return nil, err
}

var res Resource
err = extract.Into(raw.Body, &res)
return &res, err
}
20 changes: 20 additions & 0 deletions openstack/rms/query/GetResource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package query

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

func GetResource(client *golangsdk.ServiceClient, domainId, service, resourceType, id string) (*Resource, error) {
// GET /v1/resource-manager/domains/{domain_id}/provider/{provider}/type/{type}/resources/{resource_id}
raw, err := client.Get(client.ServiceURL(
"resource-manager", "domains", domainId,
"provider", service, "type", resourceType, "resources", id), nil, nil)
if err != nil {
return nil, err
}

var res Resource
err = extract.Into(raw.Body, &res)
return &res, err
}
Loading

0 comments on commit 8b82ce2

Please sign in to comment.