Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ip-version and prefix filter attributes to ReserveRandom request #378

Merged
3 commits merged into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ Some examples, more below in the actual changelog (newer entries are more likely

-->

### Added
* ipam/address: Add ip-version and prefix filter attributes to `ReserveRandom` request (#378, @anx-mschaefer)

### Fixed
* ipam: Fix several error messages to contain the correct resource (#378, @anx-mschaefer)

## [0.7.0] - 2024-05-24

* go-anxcloud is now tested with Go versions 1.21 and 1.22 and we might start using features only available since 1.21
Expand Down
44 changes: 34 additions & 10 deletions pkg/ipam/address/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,35 @@ type Create struct {
Organization string `json:"organization"`
}

// IPReserveVersionLimit limits the IP version for address reservations
type IPReserveVersionLimit int

const (
// IPReserveVersionLimitIPv4 specifies that only IPv4 addresses should be reserved
IPReserveVersionLimitIPv4 IPReserveVersionLimit = 4

// IPReserveVersionLimitIPv4 specifies that only IPv6 addresses should be reserved
IPReserveVersionLimitIPv6 IPReserveVersionLimit = 6
)

// ReserveRandom defines metadata of addresses to reserve randomly.
type ReserveRandom struct {
LocationID string `json:"location_identifier"`
VlanID string `json:"vlan_identifier"`
Count int `json:"count"`

// PrefixID limits the potential addresses to a specific prefix
// within the specified VLAN. Defaults to all prefixes within the VLAN.
PrefixID string `json:"prefix_identifier,omitempty"`

// IPVersion limits the potential addresses to a specific IP version.
// Defaults to v4 or v6, depending on availability.
IPVersion IPReserveVersionLimit `json:"ip_version,omitempty"`

// ReservationPeriod specifies how many seconds the addresses should be reserved.
nachtjasmin marked this conversation as resolved.
Show resolved Hide resolved
// If IP addresses haven't been assigned to resources within the period, they are released again.
// Defaults to 1800 seconds (= 30 minutes).
ReservationPeriod uint `json:"reservation_period,omitempty"`
}

// ReserveRandomSummary is the reserved IPs information returned by list request.
Expand Down Expand Up @@ -207,28 +231,28 @@ func (a api) Create(ctx context.Context, create Create) (Summary, error) {

requestData := bytes.Buffer{}
if err := json.NewEncoder(&requestData).Encode(create); err != nil {
panic(fmt.Sprintf("could not create request data for vlan creation: %v", err))
panic(fmt.Sprintf("could not create request data for address creation: %v", err))
}

req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, &requestData)
if err != nil {
return Summary{}, fmt.Errorf("could not create vlan post request: %w", err)
return Summary{}, fmt.Errorf("could not create address post request: %w", err)
}

httpResponse, err := a.client.Do(req)
if err != nil {
return Summary{}, fmt.Errorf("could not execute vlan post request: %w", err)
return Summary{}, fmt.Errorf("could not execute address post request: %w", err)
}
defer httpResponse.Body.Close()

if httpResponse.StatusCode >= 500 && httpResponse.StatusCode < 600 {
return Summary{}, fmt.Errorf("could not execute vlan post request, got response %s", httpResponse.Status)
return Summary{}, fmt.Errorf("could not execute address post request, got response %s", httpResponse.Status)
}

var summary Summary
err = json.NewDecoder(httpResponse.Body).Decode(&summary)
if err != nil {
return Summary{}, fmt.Errorf("could not decode vlan post response: %w", err)
return Summary{}, fmt.Errorf("could not decode address post response: %w", err)
}

return summary, nil
Expand All @@ -243,28 +267,28 @@ func (a api) Update(ctx context.Context, id string, update Update) (Summary, err

requestData := bytes.Buffer{}
if err := json.NewEncoder(&requestData).Encode(update); err != nil {
panic(fmt.Sprintf("could not create request data for vlan update: %v", err))
panic(fmt.Sprintf("could not create request data for address update: %v", err))
}

req, err := http.NewRequestWithContext(ctx, http.MethodPut, url, &requestData)
if err != nil {
return Summary{}, fmt.Errorf("could not create vlan update request: %w", err)
return Summary{}, fmt.Errorf("could not create address update request: %w", err)
}

httpResponse, err := a.client.Do(req)
if err != nil {
return Summary{}, fmt.Errorf("could not execute vlan update request: %w", err)
return Summary{}, fmt.Errorf("could not execute address update request: %w", err)
}
defer httpResponse.Body.Close()

if httpResponse.StatusCode >= 500 && httpResponse.StatusCode < 600 {
return Summary{}, fmt.Errorf("could not execute vlan update request, got response %s", httpResponse.Status)
return Summary{}, fmt.Errorf("could not execute address update request, got response %s", httpResponse.Status)
}

var summary Summary
err = json.NewDecoder(httpResponse.Body).Decode(&summary)
if err != nil {
return summary, fmt.Errorf("could not decode vlan update response: %w", err)
return summary, fmt.Errorf("could not decode address update response: %w", err)
}

return summary, err
Expand Down
42 changes: 21 additions & 21 deletions pkg/ipam/prefix/prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,23 @@ func (a api) List(ctx context.Context, page, limit int) ([]Summary, error) {

req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, fmt.Errorf("could not create vlan list request: %w", err)
return nil, fmt.Errorf("could not create prefix list request: %w", err)
}

httpResponse, err := a.client.Do(req)
if err != nil {
return nil, fmt.Errorf("could not execute vlan list request: %w", err)
return nil, fmt.Errorf("could not execute prefix list request: %w", err)
}
defer httpResponse.Body.Close()

if httpResponse.StatusCode >= 500 && httpResponse.StatusCode < 600 {
return nil, fmt.Errorf("could not execute vlan list request, got response %s", httpResponse.Status)
return nil, fmt.Errorf("could not execute prefix list request, got response %s", httpResponse.Status)
}

var responsePayload listResponse
err = json.NewDecoder(httpResponse.Body).Decode(&responsePayload)
if err != nil {
return nil, fmt.Errorf("could not decode vlan list response: %w", err)
return nil, fmt.Errorf("could not decode prefix list response: %w", err)
}

return responsePayload.Data.Data, nil
Expand All @@ -142,23 +142,23 @@ func (a api) Get(ctx context.Context, id string) (Info, error) {

req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return Info{}, fmt.Errorf("could not create vlan get request: %w", err)
return Info{}, fmt.Errorf("could not create prefix get request: %w", err)
}

httpResponse, err := a.client.Do(req)
if err != nil {
return Info{}, fmt.Errorf("could not execute vlan get request: %w", err)
return Info{}, fmt.Errorf("could not execute prefix get request: %w", err)
}
defer httpResponse.Body.Close()

if httpResponse.StatusCode >= 500 && httpResponse.StatusCode < 600 {
return Info{}, fmt.Errorf("could not execute vlan get request, got response %s", httpResponse.Status)
return Info{}, fmt.Errorf("could not execute prefix get request, got response %s", httpResponse.Status)
}

var info Info
err = json.NewDecoder(httpResponse.Body).Decode(&info)
if err != nil {
return Info{}, fmt.Errorf("could not decode vlan get response: %w", err)
return Info{}, fmt.Errorf("could not decode prefix get response: %w", err)
}

return info, nil
Expand All @@ -173,15 +173,15 @@ func (a api) Delete(ctx context.Context, id string) error {

req, err := http.NewRequestWithContext(ctx, http.MethodDelete, url, nil)
if err != nil {
return fmt.Errorf("could not create vlan delete request: %w", err)
return fmt.Errorf("could not create prefix delete request: %w", err)
}

httpResponse, err := a.client.Do(req)
if err != nil {
return fmt.Errorf("could not execute vlan delete request: %w", err)
return fmt.Errorf("could not execute prefix delete request: %w", err)
}
if httpResponse.StatusCode >= 500 && httpResponse.StatusCode < 600 {
return fmt.Errorf("could not execute vlan delete request, got response %s", httpResponse.Status)
return fmt.Errorf("could not execute prefix delete request, got response %s", httpResponse.Status)
}

return httpResponse.Body.Close()
Expand All @@ -196,27 +196,27 @@ func (a api) Create(ctx context.Context, create Create) (Summary, error) {

requestData := bytes.Buffer{}
if err := json.NewEncoder(&requestData).Encode(create); err != nil {
panic(fmt.Sprintf("could not create request data for vlan creation: %v", err))
panic(fmt.Sprintf("could not create request data for prefix creation: %v", err))
}

req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, &requestData)
if err != nil {
return Summary{}, fmt.Errorf("could not create vlan post request: %w", err)
return Summary{}, fmt.Errorf("could not create prefix post request: %w", err)
}

httpResponse, err := a.client.Do(req)
if err != nil {
return Summary{}, fmt.Errorf("could not execute vlan post request: %w", err)
return Summary{}, fmt.Errorf("could not execute prefix post request: %w", err)
}
defer httpResponse.Body.Close()
if httpResponse.StatusCode >= 500 && httpResponse.StatusCode < 600 {
return Summary{}, fmt.Errorf("could not execute vlan post request, got response %s", httpResponse.Status)
return Summary{}, fmt.Errorf("could not execute prefix post request, got response %s", httpResponse.Status)
}

var summary Summary
err = json.NewDecoder(httpResponse.Body).Decode(&summary)
if err != nil {
return Summary{}, fmt.Errorf("could not decode vlan post response: %w", err)
return Summary{}, fmt.Errorf("could not decode prefix post response: %w", err)
}

return summary, nil
Expand All @@ -231,28 +231,28 @@ func (a api) Update(ctx context.Context, id string, update Update) (Summary, err

requestData := bytes.Buffer{}
if err := json.NewEncoder(&requestData).Encode(update); err != nil {
panic(fmt.Sprintf("could not create request data for vlan update: %v", err))
panic(fmt.Sprintf("could not create request data for prefix update: %v", err))
}

req, err := http.NewRequestWithContext(ctx, http.MethodPut, url, &requestData)
if err != nil {
return Summary{}, fmt.Errorf("could not create vlan update request: %w", err)
return Summary{}, fmt.Errorf("could not create prefix update request: %w", err)
}

httpResponse, err := a.client.Do(req)
if err != nil {
return Summary{}, fmt.Errorf("could not execute vlan update request: %w", err)
return Summary{}, fmt.Errorf("could not execute prefix update request: %w", err)
}
defer httpResponse.Body.Close()

if httpResponse.StatusCode >= 500 && httpResponse.StatusCode < 600 {
return Summary{}, fmt.Errorf("could not execute vlan update request, got response %s", httpResponse.Status)
return Summary{}, fmt.Errorf("could not execute prefix update request, got response %s", httpResponse.Status)
}

var summary Summary
err = json.NewDecoder(httpResponse.Body).Decode(&summary)
if err != nil {
return summary, fmt.Errorf("could not decode vlan update response: %w", err)
return summary, fmt.Errorf("could not decode prefix update response: %w", err)
}

return summary, err
Expand Down
Loading