|
| 1 | +/* |
| 2 | +Based on: https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/gov2/ssm/GetParameter/GetParameterv2.go |
| 3 | +*/ |
| 4 | +package main |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "flag" |
| 9 | + "fmt" |
| 10 | + "log" |
| 11 | + |
| 12 | + "github.com/aws/aws-sdk-go-v2/config" |
| 13 | + "github.com/aws/aws-sdk-go-v2/service/ssm" |
| 14 | + "github.com/aws/aws-sdk-go-v2/service/ssm/types" |
| 15 | +) |
| 16 | + |
| 17 | +// SSMGetParametersAPI defines the interface for the GetParameters function. |
| 18 | +// We use this interface to test the function using a mocked service. |
| 19 | +type SSMGetParametersAPI interface { |
| 20 | + GetParameters( |
| 21 | + ctx context.Context, |
| 22 | + params *ssm.GetParametersInput, |
| 23 | + optFns ...func(*ssm.Options), |
| 24 | + ) (*ssm.GetParametersOutput, error) |
| 25 | +} |
| 26 | + |
| 27 | +type SSMDescribeParametersAPI interface { |
| 28 | + DescribeParameters( |
| 29 | + ctx context.Context, |
| 30 | + params *ssm.DescribeParametersInput, |
| 31 | + optFns ...func(*ssm.Options), |
| 32 | + ) (*ssm.DescribeParametersOutput, error) |
| 33 | +} |
| 34 | + |
| 35 | +// FindParameters retrieves an AWS Systems Manager string parameter |
| 36 | +// Inputs: |
| 37 | +// c is the context of the method call, which includes the AWS Region |
| 38 | +// api is the interface that defines the method call |
| 39 | +// input defines the input arguments to the service call. |
| 40 | +// Output: |
| 41 | +// If success, a GetParametersOutput object containing the result of the service call and nil |
| 42 | +// Otherwise, nil and an error from the call to GetParameter |
| 43 | +func FindParameters(c context.Context, api SSMGetParametersAPI, input *ssm.GetParametersInput) (*ssm.GetParametersOutput, error) { |
| 44 | + return api.GetParameters(c, input) |
| 45 | +} |
| 46 | + |
| 47 | +func FindParameterKeys(c context.Context, api SSMDescribeParametersAPI, input *ssm.DescribeParametersInput) (*ssm.DescribeParametersOutput, error) { |
| 48 | + return api.DescribeParameters(c, input) |
| 49 | +} |
| 50 | + |
| 51 | +// Split list of reports on nested lists |
| 52 | +func generateChunks(flatSlice []string, chunkSize int) [][]string { |
| 53 | + if len(flatSlice) == 0 { |
| 54 | + return nil |
| 55 | + } |
| 56 | + |
| 57 | + chunks := make([][]string, 0, len(flatSlice)/chunkSize+1) |
| 58 | + |
| 59 | + for i, v := range flatSlice { |
| 60 | + if i%chunkSize == 0 { |
| 61 | + chunks = append(chunks, make([]string, 0, chunkSize)) |
| 62 | + } |
| 63 | + chunks[len(chunks)-1] = append(chunks[len(chunks)-1], v) |
| 64 | + } |
| 65 | + |
| 66 | + return chunks |
| 67 | +} |
| 68 | + |
| 69 | +type Parameter struct { |
| 70 | + Name string |
| 71 | + Value string |
| 72 | +} |
| 73 | + |
| 74 | +func main() { |
| 75 | + var maxResults int |
| 76 | + var productTag string |
| 77 | + flag.IntVar(&maxResults, "max", 3, "The maximum number of items to return for call to AWS") |
| 78 | + flag.StringVar(&productTag, "product", "", "Product tag") |
| 79 | + flag.Parse() |
| 80 | + |
| 81 | + if productTag == "" { |
| 82 | + log.Fatalln("Please specify the tag of product") |
| 83 | + } |
| 84 | + |
| 85 | + // Load the Shared AWS Configuration (~/.aws/config) |
| 86 | + cfg, err := config.LoadDefaultConfig(context.TODO()) |
| 87 | + if err != nil { |
| 88 | + log.Fatal(err) |
| 89 | + } |
| 90 | + client := ssm.NewFromConfig(cfg) |
| 91 | + |
| 92 | + // Set parameter filters |
| 93 | + filterKey := "tag:Product" |
| 94 | + parameterFilters := []types.ParameterStringFilter{ |
| 95 | + { |
| 96 | + Key: &filterKey, |
| 97 | + Values: []string{productTag}, |
| 98 | + }, |
| 99 | + } |
| 100 | + describeInput := &ssm.DescribeParametersInput{ |
| 101 | + MaxResults: int32(maxResults), |
| 102 | + ParameterFilters: parameterFilters, |
| 103 | + } |
| 104 | + |
| 105 | + var parameterKeys []string |
| 106 | + |
| 107 | + n := 0 |
| 108 | + for { |
| 109 | + // Fetch list of parameter keys |
| 110 | + results, err := FindParameterKeys(context.TODO(), client, describeInput) |
| 111 | + if err != nil { |
| 112 | + log.Fatal(err) |
| 113 | + } |
| 114 | + for _, p := range results.Parameters { |
| 115 | + parameterKeys = append(parameterKeys, *p.Name) |
| 116 | + } |
| 117 | + |
| 118 | + // If there are no more parameters break |
| 119 | + if *&results.NextToken == nil { |
| 120 | + break |
| 121 | + } |
| 122 | + describeInput.NextToken = *&results.NextToken |
| 123 | + |
| 124 | + n++ |
| 125 | + if n >= 10 { |
| 126 | + log.Fatal("To many iterations over DescribeParameters loop") |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + var parameters []Parameter |
| 131 | + |
| 132 | + // Split slice of parameter keys to chunks by 10 (max len allowed by AWS) |
| 133 | + // and fetch values for required parameters |
| 134 | + parameterKeyChunks := generateChunks(parameterKeys, 10) |
| 135 | + for _, chunk := range parameterKeyChunks { |
| 136 | + getInput := &ssm.GetParametersInput{ |
| 137 | + Names: chunk, |
| 138 | + } |
| 139 | + results, err := FindParameters(context.TODO(), client, getInput) |
| 140 | + if err != nil { |
| 141 | + log.Fatal(err) |
| 142 | + } |
| 143 | + |
| 144 | + for _, p := range results.Parameters { |
| 145 | + parameters = append(parameters, Parameter{Name: *p.Name, Value: *p.Value}) |
| 146 | + } |
| 147 | + } |
| 148 | + fmt.Println(parameters) |
| 149 | +} |
0 commit comments