Skip to content

Commit aae7a17

Browse files
authored
Merge pull request #43 from innodreamer/main
Add Routing Table and Internet Gateway management features
2 parents 08576c7 + 9930374 commit aae7a17

File tree

7 files changed

+1447
-0
lines changed

7 files changed

+1447
-0
lines changed

openstack/errors.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,30 @@ package openstack
22

33
import (
44
"fmt"
5+
"errors"
6+
"net/http"
57

68
"github.com/cloud-barista/nhncloud-sdk-go"
79
)
810

11+
// BaseError is an error type that all other error types embed.
12+
type BaseError struct {
13+
DefaultErrString string
14+
Info string
15+
}
16+
17+
func (e BaseError) Error() string {
18+
e.DefaultErrString = "An error occurred while executing a Gophercloud request."
19+
return e.choseErrString()
20+
}
21+
22+
func (e BaseError) choseErrString() string {
23+
if e.Info != "" {
24+
return e.Info
25+
}
26+
return e.DefaultErrString
27+
}
28+
929
// ErrEndpointNotFound is the error when no suitable endpoint can be found
1030
// in the user's catalog
1131
type ErrEndpointNotFound struct{ gophercloud.BaseError }
@@ -45,3 +65,43 @@ type ErrNoPassword struct{ gophercloud.ErrInvalidInput }
4565
func (e ErrNoPassword) Error() string {
4666
return "Environment variable OS_PASSWORD needs to be set."
4767
}
68+
69+
// ErrUnexpectedResponseCode is returned by the Request method when a response code other than
70+
// those listed in OkCodes is encountered.
71+
type ErrUnexpectedResponseCode struct {
72+
BaseError
73+
URL string
74+
Method string
75+
Expected []int
76+
Actual int
77+
Body []byte
78+
ResponseHeader http.Header
79+
}
80+
81+
// ResponseCodeIs returns true if this error is or contains an ErrUnexpectedResponseCode reporting
82+
// that the request failed with the given response code. For example, this checks if a request
83+
// failed because of a 404 error:
84+
//
85+
// allServers, err := servers.List(client, servers.ListOpts{})
86+
// if gophercloud.ResponseCodeIs(err, http.StatusNotFound) {
87+
// handleNotFound()
88+
// }
89+
//
90+
// It is safe to pass a nil error, in which case this function always returns false.
91+
func ResponseCodeIs(err error, status int) bool {
92+
var codeError ErrUnexpectedResponseCode
93+
if errors.As(err, &codeError) {
94+
return codeError.Actual == status
95+
}
96+
return false
97+
}
98+
99+
// ErrTimeOut is the error type returned when an operations times out.
100+
type ErrTimeOut struct {
101+
BaseError
102+
}
103+
104+
func (e ErrTimeOut) Error() string {
105+
e.DefaultErrString = "A time out occurred"
106+
return e.choseErrString()
107+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Proof of Concepts of NHN Cloud SDK Go
2+
// NHN Cloud SDK Go is an SDK for developing NHN Cloud connection drivers that connect NHN Cloud to CB-Spider, a sub-framework of the Cloud-Barista multi-cloud project.
3+
//
4+
// * Cloud-Barista: https://github.com/cloud-barista
5+
//
6+
// Created by ETRI, 2025.08
7+
8+
package internetgateways
9+
10+
import (
11+
"github.com/cloud-barista/nhncloud-sdk-go"
12+
"github.com/cloud-barista/nhncloud-sdk-go/pagination"
13+
)
14+
15+
// ListOpts allows filtering and sorting of Internet Gateway collections
16+
type ListOpts struct {
17+
// TenantID filters by the tenant ID
18+
TenantID string `q:"tenant_id"`
19+
20+
// ID filters by the Internet Gateway ID
21+
ID string `q:"id"`
22+
23+
// Name filters by the Internet Gateway name
24+
Name string `q:"name"`
25+
26+
// ExternalNetworkID filters by the external network ID
27+
ExternalNetworkID string `q:"external_network_id"`
28+
29+
// RoutingTableID filters by the routing table ID
30+
RoutingTableID string `q:"routingtable_id"`
31+
}
32+
33+
// List returns a Pager which allows you to iterate over Internet Gateways
34+
func List(client *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
35+
q, err := gophercloud.BuildQueryString(&opts)
36+
if err != nil {
37+
return pagination.Pager{Err: err}
38+
}
39+
40+
return pagination.NewPager(client, listURL(client)+q.String(), func(r pagination.PageResult) pagination.Page {
41+
return InternetGatewayPage{pagination.LinkedPageBase{PageResult: r}}
42+
})
43+
}
44+
45+
// Get returns details about a specific Internet Gateway
46+
func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
47+
resp, err := client.Get(getURL(client, id), &r.Body, nil)
48+
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
49+
return
50+
}
51+
52+
// CreateOpts represents options for creating an Internet Gateway
53+
type CreateOpts struct {
54+
// Name is the name of the Internet Gateway
55+
Name string `json:"name" required:"true"`
56+
57+
// ExternalNetworkID is the ID of the external network to connect to
58+
ExternalNetworkID string `json:"external_network_id" required:"true"`
59+
}
60+
61+
// ToInternetGatewayCreateMap builds a request body from CreateOpts
62+
func (opts CreateOpts) ToInternetGatewayCreateMap() (map[string]interface{}, error) {
63+
return gophercloud.BuildRequestBody(opts, "internetgateway")
64+
}
65+
66+
// CreateOptsBuilder allows extensions to add additional attributes to the Create request
67+
type CreateOptsBuilder interface {
68+
ToInternetGatewayCreateMap() (map[string]interface{}, error)
69+
}
70+
71+
// Create creates a new Internet Gateway
72+
func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
73+
b, err := opts.ToInternetGatewayCreateMap()
74+
if err != nil {
75+
r.Err = err
76+
return
77+
}
78+
79+
resp, err := client.Post(createURL(client), b, &r.Body, nil)
80+
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
81+
return
82+
}
83+
84+
// Delete deletes an Internet Gateway
85+
func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) {
86+
resp, err := client.Delete(deleteURL(client, id), &gophercloud.RequestOpts{
87+
OkCodes: []int{200, 204},
88+
})
89+
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
90+
return
91+
}
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
// Proof of Concepts of NHN Cloud SDK Go
2+
// NHN Cloud SDK Go is an SDK for developing NHN Cloud connection drivers that connect NHN Cloud to CB-Spider, a sub-framework of the Cloud-Barista multi-cloud project.
3+
//
4+
// * Cloud-Barista: https://github.com/cloud-barista
5+
//
6+
// Created by ETRI, 2025.08
7+
8+
package internetgateways
9+
10+
import (
11+
"encoding/json"
12+
"time"
13+
14+
"github.com/cloud-barista/nhncloud-sdk-go"
15+
"github.com/cloud-barista/nhncloud-sdk-go/pagination"
16+
)
17+
18+
// InternetGatewayState represents possible states of an Internet Gateway
19+
type InternetGatewayState string
20+
21+
const (
22+
// StateAvailable indicates the gateway is in normal operational state
23+
StateAvailable InternetGatewayState = "available"
24+
25+
// StateUnavailable indicates the gateway is not connected to any routing table
26+
StateUnavailable InternetGatewayState = "unavailable"
27+
28+
// StateMigrating indicates the gateway is being moved to another server for maintenance
29+
StateMigrating InternetGatewayState = "migrating"
30+
31+
// StateError indicates the gateway is connected to a routing table but not functioning properly
32+
StateError InternetGatewayState = "error"
33+
)
34+
35+
// MigrateStatus represents possible migration statuses during maintenance
36+
type MigrateStatus string
37+
38+
const (
39+
// MigrateStatusNone indicates no migration in progress or migration completed
40+
MigrateStatusNone MigrateStatus = "none"
41+
42+
// MigrateStatusUnbindingProgress indicates removal from old server in progress
43+
MigrateStatusUnbindingProgress MigrateStatus = "unbinding_progress"
44+
45+
// MigrateStatusUnbindingError indicates error occurred during removal from old server
46+
MigrateStatusUnbindingError MigrateStatus = "unbinding_error"
47+
48+
// MigrateStatusBindingProgress indicates setup on new server in progress
49+
MigrateStatusBindingProgress MigrateStatus = "binding_progress"
50+
51+
// MigrateStatusBindingError indicates error occurred during setup on new server
52+
MigrateStatusBindingError MigrateStatus = "binding_error"
53+
)
54+
55+
// InternetGateway represents an Internet Gateway
56+
type InternetGateway struct {
57+
// ID is the unique identifier for the Internet Gateway
58+
ID string `json:"id"`
59+
60+
// Name is the name of the Internet Gateway
61+
Name string `json:"name"`
62+
63+
// ExternalNetworkID is the ID of the external network connected to this gateway
64+
ExternalNetworkID string `json:"external_network_id"`
65+
66+
// RoutingTableID is the ID of the routing table connected to this gateway (may be null)
67+
RoutingTableID *string `json:"routingtable_id"`
68+
69+
// State represents the current state of the Internet Gateway
70+
// Possible values: available, unavailable, migrating, error
71+
State string `json:"state"`
72+
73+
// CreateTime is the creation time of the Internet Gateway in UTC
74+
CreateTime time.Time `json:"-"`
75+
76+
// TenantID is the tenant ID that owns this Internet Gateway
77+
TenantID string `json:"tenant_id"`
78+
79+
// MigrateStatus represents the migration status during maintenance
80+
// Possible values: none, unbinding_progress, unbinding_error, binding_progress, binding_error
81+
MigrateStatus string `json:"migrate_status"`
82+
83+
// MigrateError contains error message if migration fails
84+
MigrateError *string `json:"migrate_error"`
85+
}
86+
87+
// UnmarshalJSON implements custom JSON unmarshaling for InternetGateway
88+
func (r *InternetGateway) UnmarshalJSON(b []byte) error {
89+
type tmp InternetGateway
90+
var s struct {
91+
tmp
92+
CreateTime string `json:"create_time"`
93+
}
94+
95+
err := json.Unmarshal(b, &s)
96+
if err != nil {
97+
return err
98+
}
99+
100+
*r = InternetGateway(s.tmp)
101+
102+
if s.CreateTime != "" {
103+
t, err := time.Parse("2006-01-02 15:04:05", s.CreateTime)
104+
if err != nil {
105+
return err
106+
}
107+
r.CreateTime = t
108+
}
109+
110+
return nil
111+
}
112+
113+
// InternetGatewayPage represents a single page of Internet Gateway results
114+
type InternetGatewayPage struct {
115+
pagination.LinkedPageBase
116+
}
117+
118+
// NextPageURL extracts the next page URL from the response
119+
func (r InternetGatewayPage) NextPageURL() (string, error) {
120+
var s struct {
121+
Links []gophercloud.Link `json:"internetgateways_links"`
122+
}
123+
err := r.ExtractInto(&s)
124+
if err != nil {
125+
return "", err
126+
}
127+
return gophercloud.ExtractNextURL(s.Links)
128+
}
129+
130+
// IsEmpty determines if an InternetGatewayPage contains any results
131+
func (r InternetGatewayPage) IsEmpty() (bool, error) {
132+
internetgateways, err := ExtractInternetGateways(r)
133+
return len(internetgateways) == 0, err
134+
}
135+
136+
// ExtractInternetGateways extracts Internet Gateways from a List result
137+
func ExtractInternetGateways(r pagination.Page) ([]InternetGateway, error) {
138+
var s struct {
139+
InternetGateways []InternetGateway `json:"internetgateways"`
140+
}
141+
err := (r.(InternetGatewayPage)).ExtractInto(&s)
142+
return s.InternetGateways, err
143+
}
144+
145+
// GetResult represents the result of a get operation
146+
type GetResult struct {
147+
gophercloud.Result
148+
}
149+
150+
// Extract extracts an InternetGateway from a GetResult
151+
func (r GetResult) Extract() (*InternetGateway, error) {
152+
var s struct {
153+
InternetGateway *InternetGateway `json:"internetgateway"`
154+
}
155+
err := r.ExtractInto(&s)
156+
return s.InternetGateway, err
157+
}
158+
159+
// CreateResult represents the result of a create operation
160+
type CreateResult struct {
161+
gophercloud.Result
162+
}
163+
164+
// Extract extracts an InternetGateway from a CreateResult
165+
func (r CreateResult) Extract() (*InternetGateway, error) {
166+
var s struct {
167+
InternetGateway *InternetGateway `json:"internetgateway"`
168+
}
169+
err := r.ExtractInto(&s)
170+
return s.InternetGateway, err
171+
}
172+
173+
// DeleteResult represents the result of a delete operation
174+
type DeleteResult struct {
175+
gophercloud.ErrResult
176+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package internetgateways
2+
3+
import "github.com/cloud-barista/nhncloud-sdk-go"
4+
5+
const resourcePath = "internetgateways"
6+
7+
func rootURL(c *gophercloud.ServiceClient) string {
8+
return c.ServiceURL(resourcePath)
9+
}
10+
11+
func resourceURL(c *gophercloud.ServiceClient, id string) string {
12+
return c.ServiceURL(resourcePath, id)
13+
}
14+
15+
func listURL(c *gophercloud.ServiceClient) string {
16+
return rootURL(c)
17+
}
18+
19+
func getURL(c *gophercloud.ServiceClient, id string) string {
20+
return resourceURL(c, id)
21+
}
22+
23+
func createURL(c *gophercloud.ServiceClient) string {
24+
return rootURL(c)
25+
}
26+
27+
func deleteURL(c *gophercloud.ServiceClient, id string) string {
28+
return resourceURL(c, id)
29+
}

0 commit comments

Comments
 (0)