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

     1  package pagerduty
     2  
     3  import pagerduty "github.com/PagerDuty/go-pagerduty"
     4  
     5  // Expands an array of escalation rules into []pagerduty.EscalationRules
     6  func expandEscalationRules(list []interface{}) []pagerduty.EscalationRule {
     7  	result := make([]pagerduty.EscalationRule, 0, len(list))
     8  
     9  	for _, r := range list {
    10  		rule := r.(map[string]interface{})
    11  
    12  		escalationRule := &pagerduty.EscalationRule{
    13  			Delay: uint(rule["escalation_delay_in_minutes"].(int)),
    14  		}
    15  
    16  		for _, t := range rule["target"].([]interface{}) {
    17  			target := t.(map[string]interface{})
    18  			escalationRule.Targets = append(
    19  				escalationRule.Targets,
    20  				pagerduty.APIObject{
    21  					ID:   target["id"].(string),
    22  					Type: target["type"].(string),
    23  				},
    24  			)
    25  		}
    26  
    27  		result = append(result, *escalationRule)
    28  
    29  	}
    30  
    31  	return result
    32  }
    33  
    34  // Flattens an array of []pagerduty.EscalationRule into a map[string]interface{}
    35  func flattenEscalationRules(list []pagerduty.EscalationRule) []map[string]interface{} {
    36  	result := make([]map[string]interface{}, 0, len(list))
    37  
    38  	for _, i := range list {
    39  		r := make(map[string]interface{})
    40  		r["id"] = i.ID
    41  		r["escalation_delay_in_minutes"] = i.Delay
    42  
    43  		if len(i.Targets) > 0 {
    44  			targets := make([]map[string]interface{}, 0, len(i.Targets))
    45  			for _, t := range i.Targets {
    46  				targets = append(targets, map[string]interface{}{
    47  					"id":   t.ID,
    48  					"type": t.Type,
    49  				})
    50  			}
    51  			r["target"] = targets
    52  		}
    53  
    54  		result = append(result, r)
    55  	}
    56  
    57  	return result
    58  }
    59  
    60  // Expands an array of schedules into []pagerduty.Schedule
    61  func expandScheduleLayers(list []interface{}) []pagerduty.ScheduleLayer {
    62  	result := make([]pagerduty.ScheduleLayer, 0, len(list))
    63  
    64  	for _, l := range list {
    65  		layer := l.(map[string]interface{})
    66  
    67  		scheduleLayer := &pagerduty.ScheduleLayer{
    68  			Name:                      layer["name"].(string),
    69  			Start:                     layer["start"].(string),
    70  			End:                       layer["end"].(string),
    71  			RotationVirtualStart:      layer["rotation_virtual_start"].(string),
    72  			RotationTurnLengthSeconds: uint(layer["rotation_turn_length_seconds"].(int)),
    73  		}
    74  
    75  		if layer["id"] != "" {
    76  			scheduleLayer.ID = layer["id"].(string)
    77  		}
    78  
    79  		for _, u := range layer["users"].([]interface{}) {
    80  			scheduleLayer.Users = append(
    81  				scheduleLayer.Users,
    82  				pagerduty.UserReference{
    83  					User: pagerduty.APIObject{
    84  						ID:   u.(string),
    85  						Type: "user_reference",
    86  					},
    87  				},
    88  			)
    89  		}
    90  
    91  		for _, r := range layer["restriction"].([]interface{}) {
    92  			restriction := r.(map[string]interface{})
    93  			scheduleLayer.Restrictions = append(
    94  				scheduleLayer.Restrictions,
    95  				pagerduty.Restriction{
    96  					Type:            restriction["type"].(string),
    97  					StartTimeOfDay:  restriction["start_time_of_day"].(string),
    98  					DurationSeconds: uint(restriction["duration_seconds"].(int)),
    99  				},
   100  			)
   101  		}
   102  
   103  		result = append(result, *scheduleLayer)
   104  	}
   105  
   106  	return result
   107  }
   108  
   109  // Expands an array of teams into []pagerduty.APIReference
   110  func expandTeams(list []interface{}) []pagerduty.APIReference {
   111  	result := make([]pagerduty.APIReference, 0, len(list))
   112  
   113  	for _, l := range list {
   114  		team := &pagerduty.APIReference{
   115  			ID:   l.(string),
   116  			Type: "team_reference",
   117  		}
   118  
   119  		result = append(result, *team)
   120  	}
   121  
   122  	return result
   123  }
   124  
   125  // Flattens an array of []pagerduty.ScheduleLayer into a map[string]interface{}
   126  func flattenScheduleLayers(list []pagerduty.ScheduleLayer) []map[string]interface{} {
   127  	result := make([]map[string]interface{}, 0, len(list))
   128  
   129  	for _, i := range list {
   130  		r := make(map[string]interface{})
   131  		r["id"] = i.ID
   132  		r["name"] = i.Name
   133  		r["end"] = i.End
   134  		r["start"] = i.Start
   135  		r["rotation_virtual_start"] = i.RotationVirtualStart
   136  		r["rotation_turn_length_seconds"] = i.RotationTurnLengthSeconds
   137  
   138  		if len(i.Users) > 0 {
   139  			users := make([]string, 0, len(i.Users))
   140  			for _, u := range i.Users {
   141  				users = append(users, u.User.ID)
   142  			}
   143  			r["users"] = users
   144  		}
   145  
   146  		if len(i.Restrictions) > 0 {
   147  			restrictions := make([]map[string]interface{}, 0, len(i.Restrictions))
   148  			for _, r := range i.Restrictions {
   149  				restrictions = append(restrictions, map[string]interface{}{
   150  					"duration_seconds":  r.DurationSeconds,
   151  					"start_time_of_day": r.StartTimeOfDay,
   152  					"type":              r.Type,
   153  				})
   154  			}
   155  			r["restriction"] = restrictions
   156  		}
   157  
   158  		result = append(result, r)
   159  	}
   160  
   161  	// Reverse the final result and return it
   162  	resultReversed := make([]map[string]interface{}, 0, len(result))
   163  
   164  	for i := len(result) - 1; i >= 0; i-- {
   165  		resultReversed = append(resultReversed, result[i])
   166  	}
   167  
   168  	return resultReversed
   169  }
   170  
   171  // Takes the result of flatmap.Expand for an array of strings
   172  // and returns a []string
   173  func expandStringList(configured []interface{}) []string {
   174  	vs := make([]string, 0, len(configured))
   175  	for _, v := range configured {
   176  		vs = append(vs, string(v.(string)))
   177  	}
   178  	return vs
   179  }