Skip to content
This repository has been archived by the owner on Jun 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #337 from packethost/cprivitere/issue336
Browse files Browse the repository at this point in the history
Add reservation_pricing field parsing.
  • Loading branch information
displague committed Sep 2, 2022
2 parents 05102e9 + 753c697 commit 3db2829
Showing 1 changed file with 63 additions and 13 deletions.
76 changes: 63 additions & 13 deletions plans.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package packngo

import "path"
import (
"encoding/json"
"path"
)

const planBasePath = "/plans"

Expand All @@ -17,18 +20,19 @@ type planRoot struct {

// Plan represents an Equinix Metal service plan
type Plan struct {
ID string `json:"id"`
Slug string `json:"slug,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Line string `json:"line,omitempty"`
Legacy bool `json:"legacy,omitempty"`
Specs *Specs `json:"specs,omitempty"`
Pricing *Pricing `json:"pricing,omitempty"`
DeploymentTypes []string `json:"deployment_types"`
Class string `json:"class"`
AvailableIn []Facility `json:"available_in"`
AvailableInMetros []Metro `json:"available_in_metros"`
ID string `json:"id"`
Slug string `json:"slug,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Line string `json:"line,omitempty"`
Legacy bool `json:"legacy,omitempty"`
Specs *Specs `json:"specs,omitempty"`
Pricing *Pricing `json:"pricing,omitempty"`
DeploymentTypes []string `json:"deployment_types"`
Class string `json:"class"`
AvailableIn []Facility `json:"available_in"`
AvailableInMetros []Metro `json:"available_in_metros"`
ReservationPricing *ReservationPricing `json:"reservation_pricing,omitempty"`

Href string `json:"href,omitempty"`
}
Expand All @@ -37,6 +41,52 @@ func (p Plan) String() string {
return Stringify(p)
}

type MetroPricing map[string]AnnualReservationPricing

// ReservationPricing - The reserved pricing for a plan
type ReservationPricing struct {
AnnualReservationPricing
Metros MetroPricing
}

func (r ReservationPricing) String() string {
return Stringify(r)
}

// UnmarshalJSON - Custom unmarshal function to set up the ReservationPricing object
func (r *ReservationPricing) UnmarshalJSON(data []byte) error {
var a AnnualReservationPricing
var m MetroPricing

// Leverage the built in unmarshalling to sort out all the fields
err := json.Unmarshal(data, &a)
if err != nil {
return err
}
err = json.Unmarshal(data, &m)
if err != nil {
return err
}
// Remove three_year and one_year from the metropricing object
delete(m, "three_year")
delete(m, "one_year")

// Pass the objects to the parent
r.Metros = m
r.AnnualReservationPricing = a

return nil
}

type AnnualReservationPricing struct {
OneYear *Pricing `json:"one_year"`
ThreeYear *Pricing `json:"three_year"`
}

func (m AnnualReservationPricing) String() string {
return Stringify(m)
}

// Specs - the server specs for a plan
type Specs struct {
Cpus []*Cpus `json:"cpus,omitempty"`
Expand Down

0 comments on commit 3db2829

Please sign in to comment.