github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/identity/v3/projects/results.go (about) 1 package projects 2 3 import ( 4 "encoding/json" 5 6 "github.com/vnpaycloud-console/gophercloud/v2" 7 "github.com/vnpaycloud-console/gophercloud/v2/pagination" 8 ) 9 10 // Option is a specific option defined at the API to enable features 11 // on a project. 12 type Option string 13 14 const ( 15 Immutable Option = "immutable" 16 ) 17 18 type projectResult struct { 19 gophercloud.Result 20 } 21 22 // GetResult is the result of a Get request. Call its Extract method to 23 // interpret it as a Project. 24 type GetResult struct { 25 projectResult 26 } 27 28 // CreateResult is the result of a Create request. Call its Extract method to 29 // interpret it as a Project. 30 type CreateResult struct { 31 projectResult 32 } 33 34 // DeleteResult is the result of a Delete request. Call its ExtractErr method to 35 // determine if the request succeeded or failed. 36 type DeleteResult struct { 37 gophercloud.ErrResult 38 } 39 40 // UpdateResult is the result of an Update request. Call its Extract method to 41 // interpret it as a Project. 42 type UpdateResult struct { 43 projectResult 44 } 45 46 // Project represents an OpenStack Identity Project. 47 type Project struct { 48 // IsDomain indicates whether the project is a domain. 49 IsDomain bool `json:"is_domain"` 50 51 // Description is the description of the project. 52 Description string `json:"description"` 53 54 // DomainID is the domain ID the project belongs to. 55 DomainID string `json:"domain_id"` 56 57 // Enabled is whether or not the project is enabled. 58 Enabled bool `json:"enabled"` 59 60 // ID is the unique ID of the project. 61 ID string `json:"id"` 62 63 // Name is the name of the project. 64 Name string `json:"name"` 65 66 // ParentID is the parent_id of the project. 67 ParentID string `json:"parent_id"` 68 69 // Tags is the list of tags associated with the project. 70 Tags []string `json:"tags,omitempty"` 71 72 // Extra is free-form extra key/value pairs to describe the project. 73 Extra map[string]any `json:"-"` 74 75 // Options are defined options in the API to enable certain features. 76 Options map[Option]any `json:"options,omitempty"` 77 } 78 79 func (r *Project) UnmarshalJSON(b []byte) error { 80 type tmp Project 81 var s struct { 82 tmp 83 Extra map[string]any `json:"extra"` 84 } 85 err := json.Unmarshal(b, &s) 86 if err != nil { 87 return err 88 } 89 *r = Project(s.tmp) 90 91 // Collect other fields and bundle them into Extra 92 // but only if a field titled "extra" wasn't sent. 93 if s.Extra != nil { 94 r.Extra = s.Extra 95 } else { 96 var result any 97 err := json.Unmarshal(b, &result) 98 if err != nil { 99 return err 100 } 101 if resultMap, ok := result.(map[string]any); ok { 102 r.Extra = gophercloud.RemainingKeys(Project{}, resultMap) 103 } 104 } 105 106 return err 107 } 108 109 // ProjectPage is a single page of Project results. 110 type ProjectPage struct { 111 pagination.LinkedPageBase 112 } 113 114 // IsEmpty determines whether or not a page of Projects contains any results. 115 func (r ProjectPage) IsEmpty() (bool, error) { 116 if r.StatusCode == 204 { 117 return true, nil 118 } 119 120 projects, err := ExtractProjects(r) 121 return len(projects) == 0, err 122 } 123 124 // NextPageURL extracts the "next" link from the links section of the result. 125 func (r ProjectPage) NextPageURL() (string, error) { 126 var s struct { 127 Links struct { 128 Next string `json:"next"` 129 Previous string `json:"previous"` 130 } `json:"links"` 131 } 132 err := r.ExtractInto(&s) 133 if err != nil { 134 return "", err 135 } 136 return s.Links.Next, err 137 } 138 139 // ExtractProjects returns a slice of Projects contained in a single page of 140 // results. 141 func ExtractProjects(r pagination.Page) ([]Project, error) { 142 var s struct { 143 Projects []Project `json:"projects"` 144 } 145 err := (r.(ProjectPage)).ExtractInto(&s) 146 return s.Projects, err 147 } 148 149 // Extract interprets any projectResults as a Project. 150 func (r projectResult) Extract() (*Project, error) { 151 var s struct { 152 Project *Project `json:"project"` 153 } 154 err := r.ExtractInto(&s) 155 return s.Project, err 156 } 157 158 // Tags represents a list of Tags object. 159 type Tags struct { 160 // Tags is the list of tags associated with the project. 161 Tags []string `json:"tags,omitempty"` 162 } 163 164 // ListTagsResult is the result of a List Tags request. Call its Extract method to 165 // interpret it as a list of tags. 166 type ListTagsResult struct { 167 gophercloud.Result 168 } 169 170 // Extract interprets any ListTagsResult as a Tags Object. 171 func (r ListTagsResult) Extract() (*Tags, error) { 172 var s = &Tags{} 173 err := r.ExtractInto(&s) 174 return s, err 175 } 176 177 // ProjectTags represents a list of Tags object. 178 type ProjectTags struct { 179 // Tags is the list of tags associated with the project. 180 Projects []Project `json:"projects,omitempty"` 181 // Links contains referencing links to the implied_role. 182 Links map[string]any `json:"links"` 183 } 184 185 // ModifyTagsResLinksult is the result of a Tags request. Call its Extract method to 186 // interpret it as a project of tags. 187 type ModifyTagsResult struct { 188 gophercloud.Result 189 } 190 191 // Extract interprets any ModifyTags as a Tags Object. 192 func (r ModifyTagsResult) Extract() (*ProjectTags, error) { 193 var s = &ProjectTags{} 194 err := r.ExtractInto(&s) 195 return s, err 196 } 197 198 // DeleteTagsResult is the result of a Delete Tags request. Call its ExtractErr method to 199 // determine if the request succeeded or failed. 200 type DeleteTagsResult struct { 201 gophercloud.ErrResult 202 }