github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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  					StartDayOfWeek:  uint(restriction["start_day_of_week"].(int)),
    99  					DurationSeconds: uint(restriction["duration_seconds"].(int)),
   100  				},
   101  			)
   102  		}
   103  
   104  		result = append(result, *scheduleLayer)
   105  	}
   106  
   107  	return result
   108  }
   109  
   110  // Expands an array of teams into []pagerduty.APIReference
   111  func expandTeams(list []interface{}) []pagerduty.APIReference {
   112  	result := make([]pagerduty.APIReference, 0, len(list))
   113  
   114  	for _, l := range list {
   115  		team := &pagerduty.APIReference{
   116  			ID:   l.(string),
   117  			Type: "team_reference",
   118  		}
   119  
   120  		result = append(result, *team)
   121  	}
   122  
   123  	return result
   124  }
   125  
   126  // Flattens an array of []pagerduty.ScheduleLayer into a map[string]interface{}
   127  func flattenScheduleLayers(list []pagerduty.ScheduleLayer) []map[string]interface{} {
   128  	result := make([]map[string]interface{}, 0, len(list))
   129  
   130  	for _, i := range list {
   131  		r := make(map[string]interface{})
   132  		r["id"] = i.ID
   133  		r["name"] = i.Name
   134  		r["end"] = i.End
   135  		r["start"] = i.Start
   136  		r["rotation_virtual_start"] = i.RotationVirtualStart
   137  		r["rotation_turn_length_seconds"] = i.RotationTurnLengthSeconds
   138  
   139  		if len(i.Users) > 0 {
   140  			users := make([]string, 0, len(i.Users))
   141  			for _, u := range i.Users {
   142  				users = append(users, u.User.ID)
   143  			}
   144  			r["users"] = users
   145  		}
   146  
   147  		if len(i.Restrictions) > 0 {
   148  			restrictions := make([]map[string]interface{}, 0, len(i.Restrictions))
   149  			for _, r := range i.Restrictions {
   150  				restriction := map[string]interface{}{
   151  					"duration_seconds":  r.DurationSeconds,
   152  					"start_time_of_day": r.StartTimeOfDay,
   153  					"type":              r.Type,
   154  				}
   155  
   156  				if r.StartDayOfWeek > 0 {
   157  					restriction["start_day_of_week"] = r.StartDayOfWeek
   158  				}
   159  
   160  				restrictions = append(restrictions, restriction)
   161  			}
   162  			r["restriction"] = restrictions
   163  		}
   164  
   165  		result = append(result, r)
   166  	}
   167  
   168  	// Reverse the final result and return it
   169  	resultReversed := make([]map[string]interface{}, 0, len(result))
   170  
   171  	for i := len(result) - 1; i >= 0; i-- {
   172  		resultReversed = append(resultReversed, result[i])
   173  	}
   174  
   175  	return resultReversed
   176  }
   177  
   178  // Takes the result of flatmap.Expand for an array of strings
   179  // and returns a []string
   180  func expandStringList(configured []interface{}) []string {
   181  	vs := make([]string, 0, len(configured))
   182  	for _, v := range configured {
   183  		vs = append(vs, string(v.(string)))
   184  	}
   185  	return vs
   186  }