|
| 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 | +} |
0 commit comments