diff --git a/openstack/identity/v3/agency/requests.go b/openstack/identity/v3/agency/requests.go new file mode 100644 index 000000000..4e2b341da --- /dev/null +++ b/openstack/identity/v3/agency/requests.go @@ -0,0 +1,96 @@ +package agency + +import "github.com/huaweicloud/golangsdk" + +type CreateOpts struct { + Name string `json:"name" required:"true"` + DomainID string `json:"domain_id" required:"true"` + DelegatedDomain string `json:"trust_domain_name" required:"true"` + Description string `json:"description,omitempty"` +} + +type CreateOptsBuilder interface { + ToAgencyCreateMap() (map[string]interface{}, error) +} + +func (opts CreateOpts) ToAgencyCreateMap() (map[string]interface{}, error) { + return golangsdk.BuildRequestBody(opts, "agency") +} + +func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { + b, err := opts.ToAgencyCreateMap() + if err != nil { + r.Err = err + return + } + _, r.Err = c.Post(rootURL(c), b, &r.Body, nil) + return +} + +type UpdateOpts struct { + DelegatedDomain string `json:"trust_domain_name,omitempty"` + Description string `json:"description,omitempty"` +} + +type UpdateOptsBuilder interface { + ToAgencyUpdateMap() (map[string]interface{}, error) +} + +func (opts UpdateOpts) ToAgencyUpdateMap() (map[string]interface{}, error) { + return golangsdk.BuildRequestBody(opts, "agency") +} + +func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { + b, err := opts.ToAgencyUpdateMap() + if err != nil { + r.Err = err + return + } + reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}} + _, r.Err = c.Put(resourceURL(c, id), b, &r.Body, reqOpt) + return +} + +func Get(c *golangsdk.ServiceClient, id string) (r GetResult) { + _, r.Err = c.Get(resourceURL(c, id), &r.Body, nil) + return +} + +func Delete(c *golangsdk.ServiceClient, id string) (r ErrResult) { + _, r.Err = c.Delete(resourceURL(c, id), nil) + return +} + +func AttachRoleByProject(c *golangsdk.ServiceClient, agencyID, projectID, roleID string) (r ErrResult) { + reqOpt := &golangsdk.RequestOpts{OkCodes: []int{204}} + _, r.Err = c.Put(roleURL(c, "projects", projectID, agencyID, roleID), nil, nil, reqOpt) + return +} + +func AttachRoleByDomain(c *golangsdk.ServiceClient, agencyID, domainID, roleID string) (r ErrResult) { + reqOpt := &golangsdk.RequestOpts{OkCodes: []int{204}} + _, r.Err = c.Put(roleURL(c, "domains", domainID, agencyID, roleID), nil, nil, reqOpt) + return +} + +func DetachRoleByProject(c *golangsdk.ServiceClient, agencyID, projectID, roleID string) (r ErrResult) { + reqOpt := &golangsdk.RequestOpts{OkCodes: []int{204}} + _, r.Err = c.Delete(roleURL(c, "projects", projectID, agencyID, roleID), reqOpt) + return +} + +func DetachRoleByDomain(c *golangsdk.ServiceClient, agencyID, domainID, roleID string) (r ErrResult) { + reqOpt := &golangsdk.RequestOpts{OkCodes: []int{204}} + _, r.Err = c.Delete(roleURL(c, "domains", domainID, agencyID, roleID), reqOpt) + return +} + +func ListRolesAttachedOnProject(c *golangsdk.ServiceClient, agencyID, projectID string) (r ListRolesResult) { + _, r.Err = c.Get(listRolesURL(c, "projects", projectID, agencyID), &r.Body, nil) + return +} + +func ListRolesAttachedOnDomain(c *golangsdk.ServiceClient, agencyID, domainID string) (r ListRolesResult) { + _, r.Err = c.Get(listRolesURL(c, "domains", domainID, agencyID), &r.Body, nil) + return +} diff --git a/openstack/identity/v3/agency/results.go b/openstack/identity/v3/agency/results.go new file mode 100644 index 000000000..953e98aa5 --- /dev/null +++ b/openstack/identity/v3/agency/results.go @@ -0,0 +1,58 @@ +package agency + +import ( + "github.com/huaweicloud/golangsdk" + "github.com/huaweicloud/golangsdk/openstack/identity/v3/roles" +) + +type Agency struct { + ID string `json:"id"` + Name string `json:"name"` + DomainID string `json:"domain_id"` + DelegatedDomainID string `json:"trust_domain_id"` + DelegatedDomainName string `json:"trust_domain_name"` + Description string `json:"description"` + Duration string `json:"duration"` + ExpireTime string `json:"expire_time"` + CreateTime string `json:"create_time"` +} + +type commonResult struct { + golangsdk.Result +} + +func (r commonResult) Extract() (*Agency, error) { + var s struct { + Agency *Agency `json:"agency"` + } + err := r.ExtractInto(&s) + return s.Agency, err +} + +type GetResult struct { + commonResult +} + +type CreateResult struct { + commonResult +} + +type UpdateResult struct { + commonResult +} + +type ErrResult struct { + golangsdk.ErrResult +} + +type ListRolesResult struct { + golangsdk.Result +} + +func (r ListRolesResult) ExtractRoles() ([]roles.Role, error) { + var s struct { + Roles []roles.Role `json:"roles"` + } + err := r.ExtractInto(&s) + return s.Roles, err +} diff --git a/openstack/identity/v3/agency/urls.go b/openstack/identity/v3/agency/urls.go new file mode 100644 index 000000000..cb92be2ca --- /dev/null +++ b/openstack/identity/v3/agency/urls.go @@ -0,0 +1,24 @@ +package agency + +import "github.com/huaweicloud/golangsdk" + +const ( + rootPath = "OS-AGENCY" + resourcePath = "agencies" +) + +func rootURL(c *golangsdk.ServiceClient) string { + return c.ServiceURL(rootPath, resourcePath) +} + +func resourceURL(c *golangsdk.ServiceClient, id string) string { + return c.ServiceURL(rootPath, resourcePath, id) +} + +func roleURL(c *golangsdk.ServiceClient, resource, resourceID, agencyID, roleID string) string { + return c.ServiceURL(rootPath, resource, resourceID, resourcePath, agencyID, "roles", roleID) +} + +func listRolesURL(c *golangsdk.ServiceClient, resource, resourceID, agencyID string) string { + return c.ServiceURL(rootPath, resource, resourceID, resourcePath, agencyID, "roles") +}