Skip to content

Commit

Permalink
[HSS] server group and protection status endpoints update (#748)
Browse files Browse the repository at this point in the history
anton-sidelnikov authored Nov 11, 2024
1 parent db479a6 commit 101a74f
Showing 9 changed files with 190 additions and 38 deletions.
2 changes: 1 addition & 1 deletion acceptance/clients/clients.go
Original file line number Diff line number Diff line change
@@ -172,7 +172,7 @@ func NewDCaaSV3Client() (*golangsdk.ServiceClient, error) {
if err != nil {
return nil, err
}

return openstack.NewDCaaSV3(cc.ProviderClient, golangsdk.EndpointOpts{
Region: cc.RegionName,
})
112 changes: 81 additions & 31 deletions acceptance/openstack/hss/v5/server_group_test.go
Original file line number Diff line number Diff line change
@@ -3,19 +3,22 @@ package v2
import (
"testing"

golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/acceptance/clients"
"github.com/opentelekomcloud/gophertelekomcloud/acceptance/openstack"
"github.com/opentelekomcloud/gophertelekomcloud/acceptance/tools"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/common/tags"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/compute/v2/servers"
hss "github.com/opentelekomcloud/gophertelekomcloud/openstack/hss/v5/host"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/hss/v5/host"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/hss/v5/quota"
th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
)

func TestServerGroupList(t *testing.T) {
client, err := clients.NewHssClient()
th.AssertNoErr(t, err)
tools.PrintResource(t, client)
listResp, err := hss.List(client, hss.ListOpts{})
listResp, err := host.List(client, host.ListOpts{})
th.AssertNoErr(t, err)

tools.PrintResource(t, listResp)
@@ -25,7 +28,7 @@ func TestServerList(t *testing.T) {
client, err := clients.NewHssClient()
th.AssertNoErr(t, err)
tools.PrintResource(t, client)
listResp, err := hss.ListHost(client, hss.ListHostOpts{})
listResp, err := host.ListHost(client, host.ListHostOpts{})
th.AssertNoErr(t, err)

tools.PrintResource(t, listResp)
@@ -42,19 +45,33 @@ func TestServerLifecycle(t *testing.T) {
ecsClient, err := clients.NewComputeV2Client()
ecs := openstack.CreateServer(t, ecsClient,
tools.RandomString("hss-group-member-", 3),
"Standard_Debian_10_latest",
"Standard_Debian_11_latest",
"s2.large.2",
)
th.AssertNoErr(t, err)

t.Cleanup(func() {
t.Logf("Attempting to Server group member")
t.Logf("Attempting to delete Server: %s", ecs.ID)
th.AssertNoErr(t, servers.Delete(ecsClient, ecs.ID).ExtractErr())
})

err = golangsdk.WaitFor(1000, func() (bool, error) {
h, err := host.ListHost(client, host.ListHostOpts{HostID: ecs.ID})
if err != nil {
return false, err
}

if len(h) == 1 {
return true, nil
}

return false, nil
})
th.AssertNoErr(t, err)

t.Logf("Attempting to Create Server group")
name := tools.RandomString("hss-group-", 3)
err = hss.Create(client, hss.CreateOpts{
err = host.Create(client, host.CreateOpts{
Name: name,
HostIds: []string{
ecs.ID,
@@ -63,7 +80,7 @@ func TestServerLifecycle(t *testing.T) {
th.AssertNoErr(t, err)

t.Logf("Attempting to Obtain Server group")
getResp, err := hss.List(client, hss.ListOpts{
getResp, err := host.List(client, host.ListOpts{
Name: name,
})
th.AssertNoErr(t, err)
@@ -73,11 +90,11 @@ func TestServerLifecycle(t *testing.T) {

t.Cleanup(func() {
t.Logf("Attempting to Delete Server group")
th.AssertNoErr(t, hss.Delete(client, hss.DeleteOpts{GroupID: getResp[0].ID}))
th.AssertNoErr(t, host.Delete(client, host.DeleteOpts{GroupID: getResp[0].ID}))
})

t.Logf("Attempting to Update Server group")
err = hss.Update(client, hss.UpdateOpts{
err = host.Update(client, host.UpdateOpts{
Name: name + "update",
ID: getResp[0].ID,
HostIds: []string{
@@ -87,33 +104,66 @@ func TestServerLifecycle(t *testing.T) {
th.AssertNoErr(t, err)

t.Logf("Attempting to Obtain Server group after update")
getUpdResp, err := hss.List(client, hss.ListOpts{
getUpdResp, err := host.List(client, host.ListOpts{
Name: name,
})
th.AssertNoErr(t, err)
th.AssertEquals(t, name+"update", getUpdResp[0].Name)
th.AssertEquals(t, ecs.ID, getUpdResp[0].HostIds[0])
tools.PrintResource(t, getUpdResp)

// Internal Server Error
// t.Logf("Attempting to Change server Protection Status")
// status, err := hss.ChangeProtectionStatus(client, hss.ProtectionOpts{
// Version: "hss.version.enterprise",
// HostIds: []string{
// ecs.ID,
// },
// Tags: []tags.ResourceTag{
// {
// Key: "muh",
// Value: "kuh",
// },
// {
// Key: "muh2",
// Value: "kuh2",
// },
// },
// })
// th.AssertNoErr(t, err)
// th.AssertEquals(t, "hss.version.enterprise", status.Version)
// th.AssertEquals(t, ecs.ID, status.HostIds[0])
t.Logf("Attempting to Change server Protection Status to null")
_, err = host.ChangeProtectionStatus(client, host.ProtectionOpts{
Version: "hss.version.null",
HostIds: []string{
ecs.ID,
},
Tags: []tags.ResourceTag{
{
Key: "muh",
Value: "kuh",
},
{
Key: "muh2",
Value: "kuh2",
},
},
})
th.AssertNoErr(t, err)

hs, err := host.ListHost(client, host.ListHostOpts{HostID: ecs.ID})
th.AssertNoErr(t, err)
th.AssertEquals(t, "closed", hs[0].ProtectStatus)

t.Logf("Attempting to Change server Protection Status to enterprise")
_, err = host.ChangeProtectionStatus(client, host.ProtectionOpts{
Version: "hss.version.enterprise",
// ResourceId: q[0].ResourceId,
ChargingMode: "on_demand",
HostIds: []string{
ecs.ID,
},
Tags: []tags.ResourceTag{
{
Key: "muh",
Value: "kuh",
},
{
Key: "muh2",
Value: "kuh2",
},
},
})
th.AssertNoErr(t, err)
hs, err = host.ListHost(client, host.ListHostOpts{HostID: ecs.ID})
th.AssertNoErr(t, err)
th.AssertEquals(t, "opened", hs[0].ProtectStatus)

t.Logf("Attempting to get used quota details")
q, err := quota.List(client, quota.ListOpts{
HostName: ecs.Name,
})
th.AssertNoErr(t, err)
th.AssertEquals(t, "used", q[0].UsedStatus)
th.AssertEquals(t, "hss.version.enterprise", q[0].Version)
}
2 changes: 1 addition & 1 deletion openstack/hss/v5/host/ChangeProtectionStatus.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package hss
package host

import (
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
2 changes: 1 addition & 1 deletion openstack/hss/v5/host/Create.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package hss
package host

import (
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
2 changes: 1 addition & 1 deletion openstack/hss/v5/host/Delete.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package hss
package host

import (
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
2 changes: 1 addition & 1 deletion openstack/hss/v5/host/List.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package hss
package host

import (
"bytes"
2 changes: 1 addition & 1 deletion openstack/hss/v5/host/ListHost.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package hss
package host

import (
"bytes"
2 changes: 1 addition & 1 deletion openstack/hss/v5/host/Update.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package hss
package host

import (
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
102 changes: 102 additions & 0 deletions openstack/hss/v5/quota/List.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package quota

import (
"bytes"

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

type ListOpts struct {
// Offset from which the query starts. If the value is less than 0, it is automatically converted to 0.
Offset *int `q:"offset"`
// Number of items displayed on each page.
// A value less than or equal to 0 will be automatically converted to 10,
// and a value greater than 200 will be automatically converted to 200.
Limit int `q:"limit"`
// HSS edition. Its values and their meaning are as follows:
// hss.version.null: none
// hss.version.enterprise: enterprise edition
// hss.version.premium: premium edition
// hss.version.container.enterprise: container edition
Version string `q:"version"`
// Type. Its value can be:
// host_resource
// container_resource
Category string `q:"category"`
// Quota status. It can be:
// QUOTA_STATUS_NORMAL
// QUOTA_STATUS_EXPIRED
// QUOTA_STATUS_FREEZE
QuotaStatus string `q:"quota_status"`
// Usage status. It can be:
// USED_STATUS_IDLE
// USED_STATUS_USED
UsedStatus string `q:"used_status"`
// Server name
HostName string `q:"host_name"`
// Specifies the resource ID of the HSS quota.
ResourceId string `q:"resource_id"`
// on_demand: pay-per-use
ChargingMode string `q:"charging_mode"`
}

func List(client *golangsdk.ServiceClient, opts ListOpts) ([]QuotaResp, error) {
url, err := golangsdk.NewURLBuilder().
WithEndpoints("billing", "quotas-detail").
WithQueryParams(&opts).Build()
if err != nil {
return nil, err
}

// GET /v5/{project_id}/billing/quotas-detail
pages, err := pagination.Pager{
Client: client,
InitialURL: client.ServiceURL(url.String()),
CreatePage: func(r pagination.NewPageResult) pagination.NewPage {
return QuotaPage{NewSinglePageBase: pagination.NewSinglePageBase{NewPageResult: r}}
},
}.NewAllPages()

if err != nil {
return nil, err
}
return ExtractQuotas(pages)
}

type QuotaPage struct {
pagination.NewSinglePageBase
}

func ExtractQuotas(r pagination.NewPage) ([]QuotaResp, error) {
var s struct {
Quotas []QuotaResp `json:"data_list"`
}
err := extract.Into(bytes.NewReader((r.(QuotaPage)).Body), &s)
return s.Quotas, err
}

type QuotaResp struct {
// Resource ID of an HSS quota
ResourceId string `json:"resource_id"`
// Resource specification code
Version string `json:"version"`
// Quota status
QuotaStatus string `json:"quota_status"`
// Usage status.
UsedStatus string `json:"used_status"`
// Host ID
HostId string `json:"host_id"`
// Server name
HostName string `json:"host_name"`
// on_demand: pay-per-use
ChargingMode string `json:"charging_mode"`
// Tags
Tags []tags.ResourceTag `json:"tags"`
// Expiration time. The value -1 indicates that the resource will not expire.
ExpireTime int64 `json:"expire_time"`
// Whether quotas are shared. Its value can be:
SharedQuota string `json:"shared_quota"`
}

0 comments on commit 101a74f

Please sign in to comment.