|
| 1 | +// Copyright 2015-2017 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 ssm |
| 15 | + |
| 16 | +import ( |
| 17 | + "encoding/json" |
| 18 | + |
| 19 | + "github.com/aws/amazon-ecs-cli/ecs-cli/modules/clients" |
| 20 | + "github.com/aws/amazon-ecs-cli/ecs-cli/modules/config" |
| 21 | + "github.com/aws/aws-sdk-go/aws" |
| 22 | + "github.com/aws/aws-sdk-go/service/ssm" |
| 23 | + "github.com/aws/aws-sdk-go/service/ssm/ssmiface" |
| 24 | +) |
| 25 | + |
| 26 | +const ( |
| 27 | + amazonLinuxRecommendedParameterName = "/aws/service/ecs/optimized-ami/amazon-linux/recommended" |
| 28 | +) |
| 29 | + |
| 30 | +type AMIMetadata struct { |
| 31 | + ImageID string `json:"image_id"` |
| 32 | + OsName string `json:"os"` |
| 33 | + AgentVersion string `json:"ecs_agent_version"` |
| 34 | + RuntimeVersion string `json:"ecs_runtime_version"` |
| 35 | +} |
| 36 | + |
| 37 | +// Client defines methods to interact with the SSM API interface. |
| 38 | +type Client interface { |
| 39 | + GetRecommendedECSLinuxAMI() (*AMIMetadata, error) |
| 40 | +} |
| 41 | + |
| 42 | +// ssmClient implements Client |
| 43 | +type ssmClient struct { |
| 44 | + client ssmiface.SSMAPI |
| 45 | +} |
| 46 | + |
| 47 | +// NewSSMClient creates an instance of Client. |
| 48 | +func NewSSMClient(params *config.CLIParams) Client { |
| 49 | + client := ssm.New(params.Session) |
| 50 | + client.Handlers.Build.PushBackNamed(clients.CustomUserAgentHandler()) |
| 51 | + return &ssmClient{ |
| 52 | + client: client, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func (c *ssmClient) GetRecommendedECSLinuxAMI() (*AMIMetadata, error) { |
| 57 | + response, err := c.client.GetParameter(&ssm.GetParameterInput{ |
| 58 | + Name: aws.String(amazonLinuxRecommendedParameterName), |
| 59 | + }) |
| 60 | + if err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + metadata := &AMIMetadata{} |
| 64 | + err = json.Unmarshal([]byte(aws.StringValue(response.Parameter.Value)), metadata) |
| 65 | + return metadata, err |
| 66 | +} |
0 commit comments