github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/eps/v1/enterpriseprojects/requests.go (about) 1 package enterpriseprojects 2 3 import ( 4 "github.com/chnsz/golangsdk" 5 ) 6 7 var RequestOpts = golangsdk.RequestOpts{ 8 MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"}, 9 } 10 11 type ListOpts struct { 12 Name string `q:"name"` 13 ID string `q:"id"` 14 Status int `q:"status"` 15 Type string `q:"type"` 16 SortKey string `q:"sort_key"` 17 SortDir string `q:"sort_dir"` 18 } 19 20 func (opts ListOpts) ToEnterpriseProjectListQuery() (string, error) { 21 q, err := golangsdk.BuildQueryString(opts) 22 return q.String(), err 23 } 24 25 type ListOptsBuilder interface { 26 ToEnterpriseProjectListQuery() (string, error) 27 } 28 29 func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) (r ListResult) { 30 url := rootURL(c) 31 if opts != nil { 32 query, err := opts.ToEnterpriseProjectListQuery() 33 if err != nil { 34 r.Err = err 35 } 36 url += query 37 } 38 39 _, r.Err = c.Get(url, &r.Body, nil) 40 return 41 } 42 43 // CreateOpts allows to create a enterprise project using given parameters. 44 type CreateOpts struct { 45 // A name can contain 1 to 64 characters. 46 // Only letters, digits, underscores (_), and hyphens (-) are allowed. 47 // The name must be unique in the domain and cannot include any form of 48 // the word "default" ("deFaulT", for instance). 49 Name string `json:"name" required:"true"` 50 // A description can contain a maximum of 512 characters. 51 Description string `json:"description"` 52 // Specifies the enterprise project type. 53 // The options are as follows: 54 // poc: indicates a test project. 55 // prod: indicates a commercial project. 56 Type string `json:"type,omitempty"` 57 } 58 59 // Create accepts a CreateOpts struct and uses the values to create a new enterprise project. 60 func Create(client *golangsdk.ServiceClient, opts CreateOpts) (r CreatResult) { 61 b, err := golangsdk.BuildRequestBody(opts, "") 62 if err != nil { 63 r.Err = err 64 return 65 } 66 67 _, r.Err = client.Post(rootURL(client), b, &r.Body, &golangsdk.RequestOpts{ 68 MoreHeaders: RequestOpts.MoreHeaders, 69 }) 70 return 71 } 72 73 // Get is a method to obtain the specified enterprise project by id. 74 func Get(client *golangsdk.ServiceClient, id string) (r GetResult) { 75 _, r.Err = client.Get(resourceURL(client, id), &r.Body, nil) 76 return 77 } 78 79 // Update accepts a CreateOpts struct and uses the values to Update a enterprise project. 80 func Update(client *golangsdk.ServiceClient, opts CreateOpts, id string) (r UpdateResult) { 81 b, err := golangsdk.BuildRequestBody(opts, "") 82 if err != nil { 83 r.Err = err 84 return 85 } 86 87 _, r.Err = client.Put(resourceURL(client, id), b, &r.Body, &golangsdk.RequestOpts{ 88 MoreHeaders: RequestOpts.MoreHeaders, 89 }) 90 return 91 } 92 93 type ActionOpts struct { 94 // enable: Enable an enterprise project. 95 // disable: Disable an enterprise project. 96 Action string `json:"action" required:"true"` 97 } 98 99 // Update accepts a ActionOpts struct and uses the values to enable or diaable a enterprise project. 100 func Action(client *golangsdk.ServiceClient, opts ActionOpts, id string) (r ActionResult) { 101 b, err := golangsdk.BuildRequestBody(opts, "") 102 if err != nil { 103 r.Err = err 104 return 105 } 106 107 _, r.Err = client.Post(actionURL(client, id), b, &r.Body, &golangsdk.RequestOpts{ 108 MoreHeaders: RequestOpts.MoreHeaders, 109 OkCodes: []int{204}, 110 }) 111 return 112 } 113 114 type MigrateResourceOpts struct { 115 ResourceId string `json:"resource_id" required:"true"` 116 117 ResourceType string `json:"resource_type" required:"true"` 118 // this filed is required when resource_type is bucket 119 RegionId string `json:"region_id,omitempty"` 120 121 // this filed is required when resource_type is region level 122 ProjectId string `json:"project_id,omitempty"` 123 124 // only support for EVS、EIP 125 Associated *bool `json:"associated,omitempty"` 126 } 127 128 func Migrate(client *golangsdk.ServiceClient, opts MigrateResourceOpts, id string) (r MigrateResult) { 129 b, err := golangsdk.BuildRequestBody(opts, "") 130 if err != nil { 131 r.Err = err 132 return 133 } 134 135 _, r.Err = client.Post(migrateURL(client, id), b, &r.Body, &golangsdk.RequestOpts{ 136 MoreHeaders: RequestOpts.MoreHeaders, 137 OkCodes: []int{204}, 138 }) 139 return 140 } 141 142 type ListResourcesOpts struct { 143 // Target enterprise project ID. 144 EnterpriseProjectId string `json:"-" required:"true"` 145 146 ResourceTypes []string `json:"resource_types" required:"true"` 147 148 Projects []string `json:"projects,omitempty"` 149 150 Offset int32 `json:"offset,omitempty"` 151 152 Limit int32 `json:"limit,omitempty"` 153 154 Matches []Match `json:"matches,omitempty"` 155 } 156 157 type Match struct { 158 Key string `json:"key" required:"true"` 159 160 Value string `json:"value" required:"true"` 161 } 162 163 // ListAssociatedResources is a method that used to query associated resources for specified enterprise project using 164 // given parameters. 165 func ListAssociatedResources(client *golangsdk.ServiceClient, opts ListResourcesOpts) (*FilterResult, error) { 166 b, err := golangsdk.BuildRequestBody(opts, "") 167 if err != nil { 168 return nil, err 169 } 170 171 var r FilterResult 172 _, err = client.Post(resourceFilterURL(client, opts.EnterpriseProjectId), b, &r, &golangsdk.RequestOpts{ 173 MoreHeaders: RequestOpts.MoreHeaders, 174 }) 175 return &r, err 176 }