github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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  }
   187  
   188  // Expands attribute slice to incident urgency rule, returns it and true if successful
   189  func expandIncidentUrgencyRule(incidentUrgencyList interface{}) (*pagerduty.IncidentUrgencyRule, bool) {
   190  	i := incidentUrgencyList.([]interface{})
   191  
   192  	i, ok := incidentUrgencyList.([]interface{})
   193  	if !ok {
   194  		return nil, false
   195  	}
   196  
   197  	m, ok := i[0].(map[string]interface{})
   198  	if !ok || len(m) == 0 {
   199  		return nil, false
   200  	}
   201  
   202  	iur := pagerduty.IncidentUrgencyRule{}
   203  	if val, ok := m["type"]; ok {
   204  		iur.Type = val.(string)
   205  	}
   206  	if val, ok := m["urgency"]; ok {
   207  		iur.Urgency = val.(string)
   208  	}
   209  	if val, ok := m["during_support_hours"]; ok {
   210  		iur.DuringSupportHours = expandIncidentUrgencyType(val)
   211  	}
   212  	if val, ok := m["outside_support_hours"]; ok {
   213  		iur.OutsideSupportHours = expandIncidentUrgencyType(val)
   214  	}
   215  
   216  	return &iur, true
   217  }
   218  
   219  // Expands attribute to inline model
   220  func expandActionInlineModel(inlineModelVal interface{}) *pagerduty.InlineModel {
   221  	inlineModel := pagerduty.InlineModel{}
   222  
   223  	if slice, ok := inlineModelVal.([]interface{}); ok && len(slice) == 1 {
   224  		m := slice[0].(map[string]interface{})
   225  
   226  		if val, ok := m["type"]; ok {
   227  			inlineModel.Type = val.(string)
   228  		}
   229  		if val, ok := m["name"]; ok {
   230  			inlineModel.Name = val.(string)
   231  		}
   232  	}
   233  
   234  	return &inlineModel
   235  }
   236  
   237  // Expands attribute into incident urgency type
   238  func expandIncidentUrgencyType(attribute interface{}) *pagerduty.IncidentUrgencyType {
   239  	ict := pagerduty.IncidentUrgencyType{}
   240  
   241  	slice := attribute.([]interface{})
   242  	if len(slice) != 1 {
   243  		return &ict
   244  	}
   245  
   246  	m := slice[0].(map[string]interface{})
   247  
   248  	if val, ok := m["type"]; ok {
   249  		ict.Type = val.(string)
   250  	}
   251  	if val, ok := m["urgency"]; ok {
   252  		ict.Urgency = val.(string)
   253  	}
   254  
   255  	return &ict
   256  }
   257  
   258  // Returns service's incident urgency rule as slice of length one and bool indicating success
   259  func flattenIncidentUrgencyRule(service *pagerduty.Service) ([]interface{}, bool) {
   260  	if service.IncidentUrgencyRule.Type == "" && service.IncidentUrgencyRule.Urgency == "" {
   261  		return nil, false
   262  	}
   263  
   264  	m := map[string]interface{}{
   265  		"type":    service.IncidentUrgencyRule.Type,
   266  		"urgency": service.IncidentUrgencyRule.Urgency,
   267  	}
   268  
   269  	if dsh := service.IncidentUrgencyRule.DuringSupportHours; dsh != nil {
   270  		m["during_support_hours"] = flattenIncidentUrgencyType(dsh)
   271  	}
   272  	if osh := service.IncidentUrgencyRule.OutsideSupportHours; osh != nil {
   273  		m["outside_support_hours"] = flattenIncidentUrgencyType(osh)
   274  	}
   275  
   276  	return []interface{}{m}, true
   277  }
   278  
   279  func flattenIncidentUrgencyType(iut *pagerduty.IncidentUrgencyType) []interface{} {
   280  	incidenUrgencyType := map[string]interface{}{
   281  		"type":    iut.Type,
   282  		"urgency": iut.Urgency,
   283  	}
   284  	return []interface{}{incidenUrgencyType}
   285  }
   286  
   287  // Expands attribute to support hours
   288  func expandSupportHours(attribute interface{}) (sh *pagerduty.SupportHours) {
   289  	if slice, ok := attribute.([]interface{}); ok && len(slice) >= 1 {
   290  		m := slice[0].(map[string]interface{})
   291  		sh = &pagerduty.SupportHours{}
   292  
   293  		if val, ok := m["type"]; ok {
   294  			sh.Type = val.(string)
   295  		}
   296  		if val, ok := m["time_zone"]; ok {
   297  			sh.Timezone = val.(string)
   298  		}
   299  		if val, ok := m["start_time"]; ok {
   300  			sh.StartTime = val.(string)
   301  		}
   302  		if val, ok := m["end_time"]; ok {
   303  			sh.EndTime = val.(string)
   304  		}
   305  		if val, ok := m["days_of_week"]; ok {
   306  			daysOfWeekInt := val.([]interface{})
   307  			var daysOfWeek []uint
   308  
   309  			for _, i := range daysOfWeekInt {
   310  				daysOfWeek = append(daysOfWeek, uint(i.(int)))
   311  			}
   312  
   313  			sh.DaysOfWeek = daysOfWeek
   314  		}
   315  	}
   316  
   317  	return
   318  }
   319  
   320  // Returns service's support hours as slice of length one
   321  func flattenSupportHours(service *pagerduty.Service) []interface{} {
   322  	if service.SupportHours == nil {
   323  		return nil
   324  	}
   325  
   326  	m := map[string]interface{}{}
   327  
   328  	if s := service.SupportHours; s != nil {
   329  		m["type"] = s.Type
   330  		m["time_zone"] = s.Timezone
   331  		m["start_time"] = s.StartTime
   332  		m["end_time"] = s.EndTime
   333  		m["days_of_week"] = s.DaysOfWeek
   334  	}
   335  
   336  	return []interface{}{m}
   337  }
   338  
   339  // Expands attribute to scheduled action
   340  func expandScheduledActions(input interface{}) (scheduledActions []pagerduty.ScheduledAction) {
   341  	inputs := input.([]interface{})
   342  
   343  	for _, i := range inputs {
   344  		m := i.(map[string]interface{})
   345  		sa := pagerduty.ScheduledAction{}
   346  
   347  		if val, ok := m["type"]; ok {
   348  			sa.Type = val.(string)
   349  		}
   350  		if val, ok := m["to_urgency"]; ok {
   351  			sa.ToUrgency = val.(string)
   352  		}
   353  		if val, ok := m["at"]; ok {
   354  			sa.At = *expandActionInlineModel(val)
   355  		}
   356  
   357  		scheduledActions = append(scheduledActions, sa)
   358  	}
   359  
   360  	return scheduledActions
   361  }
   362  
   363  // Returns service's scheduled actions
   364  func flattenScheduledActions(service *pagerduty.Service) []interface{} {
   365  	scheduledActions := []interface{}{}
   366  
   367  	if sas := service.ScheduledActions; sas != nil {
   368  		for _, sa := range sas {
   369  			m := map[string]interface{}{}
   370  			m["to_urgency"] = sa.ToUrgency
   371  			m["type"] = sa.Type
   372  			if at, ok := scheduledActionsAt(sa.At); ok {
   373  				m["at"] = at
   374  			}
   375  			scheduledActions = append(scheduledActions, m)
   376  		}
   377  	}
   378  
   379  	return scheduledActions
   380  }
   381  
   382  // Returns service's scheduled action's at attribute as slice of length one
   383  func scheduledActionsAt(inlineModel pagerduty.InlineModel) ([]interface{}, bool) {
   384  	if inlineModel.Type == "" || inlineModel.Name == "" {
   385  		return nil, false
   386  	}
   387  
   388  	m := map[string]interface{}{"type": inlineModel.Type, "name": inlineModel.Name}
   389  	return []interface{}{m}, true
   390  }