github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/identity/federatedauth/mappings/requests.go (about) 1 package mappings 2 3 import ( 4 "github.com/chnsz/golangsdk" 5 "github.com/chnsz/golangsdk/pagination" 6 ) 7 8 var RequestOpts = golangsdk.RequestOpts{ 9 MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"}, 10 } 11 12 type MappingOption struct { 13 Rules []MappingRule `json:"rules" required:"true"` 14 } 15 16 type MappingRule struct { 17 Local []LocalRule `json:"local" required:"true"` 18 Remote []RemoteRule `json:"remote" required:"true"` 19 } 20 21 type LocalRule struct { 22 User LocalRuleVal `json:"user" required:"true"` 23 Group *LocalRuleVal `json:"group,omitempty"` 24 } 25 26 type LocalRuleVal struct { 27 Name string `json:"name"` 28 } 29 30 type RemoteRule struct { 31 Type string `json:"type" required:"true"` 32 AnyOneOf []string `json:"any_one_of,omitempty"` 33 NotAnyOf []string `json:"not_any_of,omitempty"` 34 } 35 36 type responseBody struct { 37 Mapping IdentityMapping `json:"mapping"` 38 } 39 40 func Create(c *golangsdk.ServiceClient, id string, opts MappingOption) (*IdentityMapping, error) { 41 b, err := golangsdk.BuildRequestBody(opts, "mapping") 42 if err != nil { 43 return nil, err 44 } 45 46 var rst golangsdk.Result 47 _, err = c.Put(resourceURL(c, id), b, &rst.Body, &golangsdk.RequestOpts{ 48 MoreHeaders: RequestOpts.MoreHeaders, 49 }) 50 51 if err == nil { 52 var r responseBody 53 rst.ExtractInto(&r) 54 return &r.Mapping, nil 55 } 56 return nil, err 57 } 58 59 func Get(c *golangsdk.ServiceClient, id string) (*IdentityMapping, error) { 60 var rst golangsdk.Result 61 _, err := c.Get(resourceURL(c, id), &rst.Body, &golangsdk.RequestOpts{ 62 MoreHeaders: RequestOpts.MoreHeaders, 63 }) 64 if err == nil { 65 var r responseBody 66 rst.ExtractInto(&r) 67 return &r.Mapping, nil 68 } 69 return nil, err 70 } 71 72 func List(client *golangsdk.ServiceClient) pagination.Pager { 73 return pagination.NewPager(client, listURL(client), func(r pagination.PageResult) pagination.Page { 74 return IdentityMappingPage{pagination.LinkedPageBase{PageResult: r}} 75 }) 76 } 77 78 func Update(c *golangsdk.ServiceClient, id string, opts MappingOption) (*IdentityMapping, error) { 79 b, err := golangsdk.BuildRequestBody(opts, "mapping") 80 if err != nil { 81 return nil, err 82 } 83 84 var rst golangsdk.Result 85 _, err = c.Patch(resourceURL(c, id), b, &rst.Body, &golangsdk.RequestOpts{ 86 MoreHeaders: RequestOpts.MoreHeaders, 87 }) 88 if err == nil { 89 var r responseBody 90 rst.ExtractInto(&r) 91 return &r.Mapping, nil 92 } 93 return nil, err 94 } 95 96 func Delete(c *golangsdk.ServiceClient, id string) error { 97 _, err := c.Delete(resourceURL(c, id), &golangsdk.RequestOpts{ 98 MoreHeaders: RequestOpts.MoreHeaders, 99 }) 100 return err 101 }