|
| 1 | +// Copyright 2015-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). You may |
| 4 | +// not use this file except in compliance with the License. A copy of the |
| 5 | +// License is located at |
| 6 | +// |
| 7 | +// http://aws.amazon.com/apache2.0/ |
| 8 | +// |
| 9 | +// or in the "license" file accompanying this file. This file is distributed |
| 10 | +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +// express or implied. See the License for the specific language governing |
| 12 | +// permissions and limitations under the License. |
| 13 | + |
| 14 | +package iam |
| 15 | + |
| 16 | +import ( |
| 17 | + "github.com/aws/amazon-ecs-cli/ecs-cli/modules/clients" |
| 18 | + "github.com/aws/amazon-ecs-cli/ecs-cli/modules/config" |
| 19 | + "github.com/aws/aws-sdk-go/aws" |
| 20 | + "github.com/aws/aws-sdk-go/service/iam" |
| 21 | + "github.com/aws/aws-sdk-go/service/iam/iamiface" |
| 22 | +) |
| 23 | + |
| 24 | +// Client defines methods for interacting with the IAMAPI interface |
| 25 | +type Client interface { |
| 26 | + AttachRolePolicy(policyArn, roleName string) (*iam.AttachRolePolicyOutput, error) |
| 27 | + CreateRole(iam.CreateRoleInput) (*iam.CreateRoleOutput, error) |
| 28 | + CreatePolicy(iam.CreatePolicyInput) (*iam.CreatePolicyOutput, error) |
| 29 | +} |
| 30 | + |
| 31 | +type iamClient struct { |
| 32 | + client iamiface.IAMAPI |
| 33 | +} |
| 34 | + |
| 35 | +// NewIAMClient creates an instance of iamClient |
| 36 | +func NewIAMClient(config *config.CommandConfig) Client { |
| 37 | + client := iam.New(config.Session) |
| 38 | + client.Handlers.Build.PushBackNamed(clients.CustomUserAgentHandler()) |
| 39 | + |
| 40 | + return newClient(client) |
| 41 | +} |
| 42 | + |
| 43 | +func newClient(client iamiface.IAMAPI) Client { |
| 44 | + return &iamClient{ |
| 45 | + client: client, |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func (c *iamClient) AttachRolePolicy(policyArn, roleName string) (*iam.AttachRolePolicyOutput, error) { |
| 50 | + request := iam.AttachRolePolicyInput{ |
| 51 | + PolicyArn: aws.String(policyArn), |
| 52 | + RoleName: aws.String(roleName), |
| 53 | + } |
| 54 | + |
| 55 | + output, err := c.client.AttachRolePolicy(&request) |
| 56 | + if err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + |
| 60 | + return output, nil |
| 61 | +} |
| 62 | + |
| 63 | +func (c *iamClient) CreateRole(input iam.CreateRoleInput) (*iam.CreateRoleOutput, error) { |
| 64 | + output, err := c.client.CreateRole(&input) |
| 65 | + if err != nil { |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + |
| 69 | + return output, nil |
| 70 | +} |
| 71 | + |
| 72 | +func (c *iamClient) CreatePolicy(input iam.CreatePolicyInput) (*iam.CreatePolicyOutput, error) { |
| 73 | + output, err := c.client.CreatePolicy(&input) |
| 74 | + if err != nil { |
| 75 | + return nil, err |
| 76 | + } |
| 77 | + |
| 78 | + return output, nil |
| 79 | +} |
0 commit comments