assume_role.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package sts
  2. //Licensed under the Apache License, Version 2.0 (the "License");
  3. //you may not use this file except in compliance with the License.
  4. //You may obtain a copy of the License at
  5. //
  6. //http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. //Unless required by applicable law or agreed to in writing, software
  9. //distributed under the License is distributed on an "AS IS" BASIS,
  10. //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. //See the License for the specific language governing permissions and
  12. //limitations under the License.
  13. //
  14. // Code generated by Alibaba Cloud SDK Code Generator.
  15. // Changes may cause incorrect behavior and will be lost if the code is regenerated.
  16. import (
  17. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
  18. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses"
  19. )
  20. // AssumeRole invokes the sts.AssumeRole API synchronously
  21. // api document: https://help.aliyun.com/api/sts/assumerole.html
  22. func (client *Client) AssumeRole(request *AssumeRoleRequest) (response *AssumeRoleResponse, err error) {
  23. response = CreateAssumeRoleResponse()
  24. err = client.DoAction(request, response)
  25. return
  26. }
  27. // AssumeRoleWithChan invokes the sts.AssumeRole API asynchronously
  28. // api document: https://help.aliyun.com/api/sts/assumerole.html
  29. // asynchronous document: https://help.aliyun.com/document_detail/66220.html
  30. func (client *Client) AssumeRoleWithChan(request *AssumeRoleRequest) (<-chan *AssumeRoleResponse, <-chan error) {
  31. responseChan := make(chan *AssumeRoleResponse, 1)
  32. errChan := make(chan error, 1)
  33. err := client.AddAsyncTask(func() {
  34. defer close(responseChan)
  35. defer close(errChan)
  36. response, err := client.AssumeRole(request)
  37. if err != nil {
  38. errChan <- err
  39. } else {
  40. responseChan <- response
  41. }
  42. })
  43. if err != nil {
  44. errChan <- err
  45. close(responseChan)
  46. close(errChan)
  47. }
  48. return responseChan, errChan
  49. }
  50. // AssumeRoleWithCallback invokes the sts.AssumeRole API asynchronously
  51. // api document: https://help.aliyun.com/api/sts/assumerole.html
  52. // asynchronous document: https://help.aliyun.com/document_detail/66220.html
  53. func (client *Client) AssumeRoleWithCallback(request *AssumeRoleRequest, callback func(response *AssumeRoleResponse, err error)) <-chan int {
  54. result := make(chan int, 1)
  55. err := client.AddAsyncTask(func() {
  56. var response *AssumeRoleResponse
  57. var err error
  58. defer close(result)
  59. response, err = client.AssumeRole(request)
  60. callback(response, err)
  61. result <- 1
  62. })
  63. if err != nil {
  64. defer close(result)
  65. callback(nil, err)
  66. result <- 0
  67. }
  68. return result
  69. }
  70. // AssumeRoleRequest is the request struct for api AssumeRole
  71. type AssumeRoleRequest struct {
  72. *requests.RpcRequest
  73. RoleSessionName string `position:"Query" name:"RoleSessionName"`
  74. Policy string `position:"Query" name:"Policy"`
  75. RoleArn string `position:"Query" name:"RoleArn"`
  76. DurationSeconds requests.Integer `position:"Query" name:"DurationSeconds"`
  77. }
  78. // AssumeRoleResponse is the response struct for api AssumeRole
  79. type AssumeRoleResponse struct {
  80. *responses.BaseResponse
  81. RequestId string `json:"RequestId" xml:"RequestId"`
  82. Credentials Credentials `json:"Credentials" xml:"Credentials"`
  83. AssumedRoleUser AssumedRoleUser `json:"AssumedRoleUser" xml:"AssumedRoleUser"`
  84. }
  85. // CreateAssumeRoleRequest creates a request to invoke AssumeRole API
  86. func CreateAssumeRoleRequest() (request *AssumeRoleRequest) {
  87. request = &AssumeRoleRequest{
  88. RpcRequest: &requests.RpcRequest{},
  89. }
  90. request.InitWithApiInfo("Sts", "2015-04-01", "AssumeRole", "sts", "openAPI")
  91. return
  92. }
  93. // CreateAssumeRoleResponse creates a response to parse from AssumeRole response
  94. func CreateAssumeRoleResponse() (response *AssumeRoleResponse) {
  95. response = &AssumeRoleResponse{
  96. BaseResponse: &responses.BaseResponse{},
  97. }
  98. return
  99. }