github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/iam_policy_model.go (about)

     1  package aws
     2  
     3  import (
     4  	"encoding/json"
     5  	"sort"
     6  )
     7  
     8  type IAMPolicyDoc struct {
     9  	Version    string                `json:",omitempty"`
    10  	Id         string                `json:",omitempty"`
    11  	Statements []*IAMPolicyStatement `json:"Statement"`
    12  }
    13  
    14  type IAMPolicyStatement struct {
    15  	Sid           string
    16  	Effect        string                         `json:",omitempty"`
    17  	Actions       interface{}                    `json:"Action,omitempty"`
    18  	NotActions    interface{}                    `json:"NotAction,omitempty"`
    19  	Resources     interface{}                    `json:"Resource,omitempty"`
    20  	NotResources  interface{}                    `json:"NotResource,omitempty"`
    21  	Principals    IAMPolicyStatementPrincipalSet `json:"Principal,omitempty"`
    22  	NotPrincipals IAMPolicyStatementPrincipalSet `json:"NotPrincipal,omitempty"`
    23  	Conditions    IAMPolicyStatementConditionSet `json:"Condition,omitempty"`
    24  }
    25  
    26  type IAMPolicyStatementPrincipal struct {
    27  	Type        string
    28  	Identifiers interface{}
    29  }
    30  
    31  type IAMPolicyStatementCondition struct {
    32  	Test     string
    33  	Variable string
    34  	Values   interface{}
    35  }
    36  
    37  type IAMPolicyStatementPrincipalSet []IAMPolicyStatementPrincipal
    38  type IAMPolicyStatementConditionSet []IAMPolicyStatementCondition
    39  
    40  func (ps IAMPolicyStatementPrincipalSet) MarshalJSON() ([]byte, error) {
    41  	raw := map[string]interface{}{}
    42  
    43  	for _, p := range ps {
    44  		switch i := p.Identifiers.(type) {
    45  		case []string:
    46  			if _, ok := raw[p.Type]; !ok {
    47  				raw[p.Type] = make([]string, 0, len(i))
    48  			}
    49  			sort.Sort(sort.Reverse(sort.StringSlice(i)))
    50  			raw[p.Type] = append(raw[p.Type].([]string), i...)
    51  		case string:
    52  			raw[p.Type] = i
    53  		default:
    54  			panic("Unsupported data type for IAMPolicyStatementPrincipalSet")
    55  		}
    56  	}
    57  
    58  	return json.Marshal(&raw)
    59  }
    60  
    61  func (cs IAMPolicyStatementConditionSet) MarshalJSON() ([]byte, error) {
    62  	raw := map[string]map[string]interface{}{}
    63  
    64  	for _, c := range cs {
    65  		if _, ok := raw[c.Test]; !ok {
    66  			raw[c.Test] = map[string]interface{}{}
    67  		}
    68  		switch i := c.Values.(type) {
    69  		case []string:
    70  			if _, ok := raw[c.Test][c.Variable]; !ok {
    71  				raw[c.Test][c.Variable] = make([]string, 0, len(i))
    72  			}
    73  			sort.Sort(sort.Reverse(sort.StringSlice(i)))
    74  			raw[c.Test][c.Variable] = append(raw[c.Test][c.Variable].([]string), i...)
    75  		case string:
    76  			raw[c.Test][c.Variable] = i
    77  		default:
    78  			panic("Unsupported data type for IAMPolicyStatementConditionSet")
    79  		}
    80  	}
    81  
    82  	return json.Marshal(&raw)
    83  }
    84  
    85  func iamPolicyDecodeConfigStringList(lI []interface{}) interface{} {
    86  	if len(lI) == 1 {
    87  		return lI[0].(string)
    88  	}
    89  	ret := make([]string, len(lI))
    90  	for i, vI := range lI {
    91  		ret[i] = vI.(string)
    92  	}
    93  	sort.Sort(sort.Reverse(sort.StringSlice(ret)))
    94  	return ret
    95  }