github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/eg/v1/channel/custom/requests.go (about) 1 package custom 2 3 import ( 4 "fmt" 5 6 "github.com/chnsz/golangsdk" 7 "github.com/chnsz/golangsdk/pagination" 8 ) 9 10 // CreateOpts is the structure used to create a new custom channel. 11 type CreateOpts struct { 12 // The name of the custom channel. 13 Name string `json:"name" required:"true"` 14 // The description of the custom channel. 15 Description string `json:"description,omitempty"` 16 // The ID of the enterprise project to which the custom channel belongs. 17 EnterpriseProjectId string `json:"eps_id,omitempty" q:"enterprise_project_id"` 18 // Whether enable cross-account configuration. 19 CrossAccount *bool `json:"cross_account,omitempty"` 20 // The event policy of the cross-account. 21 Policy *CrossAccountPolicy `json:"policy,omitempty"` 22 } 23 24 var requestOpts = golangsdk.RequestOpts{ 25 MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"}, 26 } 27 28 // Create is a method used to create a new custom channel using given parameters. 29 func Create(client *golangsdk.ServiceClient, opts CreateOpts) (*Channel, error) { 30 b, err := golangsdk.BuildRequestBody(opts, "") 31 if err != nil { 32 return nil, err 33 } 34 url := rootURL(client) 35 query, err := golangsdk.BuildQueryString(opts) 36 if err != nil { 37 return nil, err 38 } 39 url += query.String() 40 41 var r Channel 42 _, err = client.Post(url, b, &r, &golangsdk.RequestOpts{ 43 MoreHeaders: requestOpts.MoreHeaders, 44 }) 45 return &r, err 46 } 47 48 // Get is a method to query an existing channel by its ID. 49 func Get(client *golangsdk.ServiceClient, channelId, epsId string) (*Channel, error) { 50 var r Channel 51 url := resourceURL(client, channelId) 52 if epsId != "" { 53 url = fmt.Sprintf("%s?enterprise_project_id=%s", url, epsId) 54 } 55 _, err := client.Get(url, &r, &golangsdk.RequestOpts{ 56 MoreHeaders: requestOpts.MoreHeaders, 57 }) 58 return &r, err 59 } 60 61 // ListOpts is the structure used to query channel list. 62 type ListOpts struct { 63 // The ID of the event channel to which the channel belongs. 64 ChannelId string `q:"channel_id"` 65 // The ID of the enterprise project to which the custom channel belongs. 66 EnterpriseProjectId string `q:"enterprise_project_id"` 67 // Offset from which the query starts. 68 // If the offset is less than 0, the value is automatically converted to 0. 69 // The valid value ranges from 0 to 100, defaults to 0. 70 Offset int `q:"offset"` 71 // Number of items displayed on each page. 72 // The valid value ranges from 1 to 1000, defaults to 15. 73 Limit int `q:"limit"` 74 // The query sorting. 75 // The default value is 'created_time:DESC'. 76 Sort string `q:"sort"` 77 // The type of the channel provider. 78 // + OFFICIAL: official cloud service channel. 79 // + CUSTOM: the user-defined channel. 80 // + PARTNER: partner channel. 81 ProviderType string `q:"provider_type"` 82 // The name of the channel. 83 Name string `q:"name"` 84 // The fuzzy name of the channel. 85 FuzzyName string `q:"fuzzy_name"` 86 } 87 88 // List is a method to query the channel list using given parameters. 89 func List(client *golangsdk.ServiceClient, opts ListOpts) ([]Channel, error) { 90 url := rootURL(client) 91 query, err := golangsdk.BuildQueryString(opts) 92 if err != nil { 93 return nil, err 94 } 95 url += query.String() 96 97 pages, err := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 98 p := ChannelPage{pagination.OffsetPageBase{PageResult: r}} 99 return p 100 }).AllPages() 101 102 if err != nil { 103 return nil, err 104 } 105 return ExtractChannels(pages) 106 } 107 108 // UpdateOpts is the structure used to update an existing custom channel. 109 type UpdateOpts struct { 110 // The ID of the channel. 111 ChannelId string `json:"-" required:"true"` 112 // The description of the channel. 113 Description *string `json:"description,omitempty"` 114 // Whether enable cross-account configuration. 115 CrossAccount *bool `json:"cross_account,omitempty"` 116 // The event policy of the cross-account. 117 Policy *CrossAccountPolicy `json:"policy,omitempty"` 118 // The ID of the enterprise project to which the custom channel belongs. 119 // Notes: this parameter does not support update, but it is required for request body and query parameter. 120 EnterpriseProjectId string `json:"eps_id,omitempty" q:"enterprise_project_id"` 121 } 122 123 // CrossAccountPolicy is the structure that represents the cross-account policy. 124 type CrossAccountPolicy struct { 125 // The SID of the cross-account policy. 126 Sid string `json:"Sid"` 127 // The effect of the cross-account policy. 128 // + Allow 129 // + Deny 130 Effect string `json:"Effect"` 131 // The configuration of the IAM account. 132 Principal PrincipalInfo `json:"Principal"` 133 // The action of the cross-account policy, such as 'eg:channels:putEvents'. 134 Action string `json:"Action"` 135 // The URN of the custom channel. 136 // The format is 'urn:eg:{region}:{domain_id}:channel:{channel_name}' 137 // Before channel created, the channel name is empty. 138 Resource string `json:"Resource"` 139 } 140 141 // PrincipalInfo is the structure that represents the domain ID list of the cross-account policy. 142 type PrincipalInfo struct { 143 // The account IDs. 144 IAM []string `json:"IAM"` 145 } 146 147 // Update is a method used to modify an existing custom channel using given parameters. 148 func Update(client *golangsdk.ServiceClient, opts UpdateOpts) (*Channel, error) { 149 b, err := golangsdk.BuildRequestBody(opts, "") 150 if err != nil { 151 return nil, err 152 } 153 url := resourceURL(client, opts.ChannelId) 154 query, err := golangsdk.BuildQueryString(opts) 155 if err != nil { 156 return nil, err 157 } 158 url += query.String() 159 160 var r Channel 161 _, err = client.Put(url, b, &r, &golangsdk.RequestOpts{ 162 MoreHeaders: requestOpts.MoreHeaders, 163 }) 164 return &r, err 165 } 166 167 // Delete is a method to delete an existing custom channel using its ID. 168 func Delete(client *golangsdk.ServiceClient, channelId, epsId string) error { 169 url := resourceURL(client, channelId) 170 if epsId != "" { 171 url = fmt.Sprintf("%s?enterprise_project_id=%s", url, epsId) 172 } 173 _, err := client.Delete(url, nil) 174 return err 175 }