github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/networking/v2/vpcs/results.go (about) 1 package vpcs 2 3 import ( 4 "encoding/json" 5 "time" 6 7 "github.com/vnpaycloud-console/gophercloud/v2" 8 "github.com/vnpaycloud-console/gophercloud/v2/pagination" 9 ) 10 11 type commonResult struct { 12 gophercloud.Result 13 } 14 15 // Extract is a function that accepts a result and extracts a VPC resource. 16 func (r commonResult) Extract() (*VPC, error) { 17 var s VPC 18 err := r.ExtractInto(&s) 19 return &s, err 20 } 21 22 func (r commonResult) ExtractInto(v any) error { 23 return r.Result.ExtractIntoStructPtr(v, "vpc") 24 } 25 26 // CreateResult represents the result of a create operation. Call its Extract 27 // method to interpret it as a VPC. 28 type CreateResult struct { 29 commonResult 30 } 31 32 // GetResult represents the result of a get operation. Call its Extract 33 // method to interpret it as a VPC. 34 type GetResult struct { 35 commonResult 36 } 37 38 // UpdateResult represents the result of an update operation. Call its Extract 39 // method to interpret it as a VPC. 40 type UpdateResult struct { 41 commonResult 42 } 43 44 // DeleteResult represents the result of a delete operation. Call its 45 // ExtractErr method to determine if the request succeeded or failed. 46 type DeleteResult struct { 47 gophercloud.ErrResult 48 } 49 50 // VPC represents, well, a VPC. 51 type VPC struct { 52 ID string `json:"id"` 53 Name string `json:"name"` 54 Description string `json:"description"` 55 CIDR string `json:"cidr"` 56 SNATAddress string `json:"snat_address"` 57 EnableSNAT bool `json:"enable_snat"` 58 Region string `json:"region"` 59 UpdatedAt time.Time `json:"-"` 60 CreatedAt time.Time `json:"-"` 61 ProjectID string `json:"project_id"` 62 Status string `json:"status"` 63 } 64 65 func (r *VPC) UnmarshalJSON(b []byte) error { 66 type tmp VPC 67 68 // Support for older neutron time format 69 var s1 struct { 70 tmp 71 CreatedAt gophercloud.JSONRFC3339NoZ `json:"created_at"` 72 UpdatedAt gophercloud.JSONRFC3339NoZ `json:"updated_at"` 73 } 74 75 err := json.Unmarshal(b, &s1) 76 if err == nil { 77 *r = VPC(s1.tmp) 78 r.CreatedAt = time.Time(s1.CreatedAt) 79 r.UpdatedAt = time.Time(s1.UpdatedAt) 80 81 return nil 82 } 83 84 // Support for newer neutron time format 85 var s2 struct { 86 tmp 87 CreatedAt time.Time `json:"created_at"` 88 UpdatedAt time.Time `json:"updated_at"` 89 } 90 91 err = json.Unmarshal(b, &s2) 92 if err != nil { 93 return err 94 } 95 96 *r = VPC(s2.tmp) 97 r.CreatedAt = time.Time(s2.CreatedAt) 98 r.UpdatedAt = time.Time(s2.UpdatedAt) 99 100 return nil 101 } 102 103 // VPCPage is the page returned by a pager when traversing over a 104 // collection of VPCs. 105 type VPCPage struct { 106 pagination.LinkedPageBase 107 } 108 109 // NextPageURL is invoked when a paginated collection of VPCs has 110 // reached the end of a page and a new request is needed to fetch 111 // the next page of VPCs. It returns the URL to use for the next 112 // request. 113 func (r VPCPage) NextPageURL() (string, error) { 114 var s struct { 115 Links []gophercloud.Link `json:"vpcs_links"` 116 } 117 err := r.ExtractInto(&s) 118 if err != nil { 119 return "", err 120 } 121 return gophercloud.ExtractNextURL(s.Links) 122 } 123 124 // IsEmpty returns true if a VPCPage contains no VPCs. 125 func (r VPCPage) IsEmpty() (bool, error) { 126 vpcs, err := ExtractVPCs(r) 127 if err != nil { 128 return true, err 129 } 130 return len(vpcs) == 0, nil 131 } 132 133 // ExtractVPCs accepts a Page struct, specifically a VPCPage struct, and 134 // extracts the elements into a slice of VPC structs. In other words, a 135 // VPCPage contains a collection of VPC structs. 136 func ExtractVPCs(r pagination.Page) ([]VPC, error) { 137 var s []VPC 138 err := ExtractVPCsInto(r, &s) 139 if err != nil { 140 return nil, err 141 } 142 return s, nil 143 } 144 145 func ExtractVPCsInto(r pagination.Page, v any) error { 146 return r.(VPCPage).ExtractIntoSlicePtr(v, "vpcs") 147 }