refactor: remove resource-type enums from export commands (#137)

This commit is contained in:
Daniel Moran
2021-06-22 09:10:03 -04:00
committed by GitHub
parent 800e4c2cfb
commit a029bf2871
12 changed files with 91 additions and 407 deletions

View File

@ -18,24 +18,8 @@ type Params struct {
OutParams
StackId string
BucketIds []string
BucketNames []string
CheckIds []string
CheckNames []string
DashboardIds []string
DashboardNames []string
EndpointIds []string
EndpointNames []string
LabelIds []string
LabelNames []string
RuleIds []string
RuleNames []string
TaskIds []string
TaskNames []string
TelegrafIds []string
TelegrafNames []string
VariableIds []string
VariableNames []string
IdsPerType map[string][]string
NamesPerType map[string][]string
}
func (c Client) Export(ctx context.Context, params *Params) error {
@ -45,70 +29,20 @@ func (c Client) Export(ctx context.Context, params *Params) error {
exportReq.StackID = &params.StackId
}
filters := []struct {
kind api.TemplateKind
ids []string
names []string
}{
{
kind: api.TEMPLATEKIND_BUCKET,
ids: params.BucketIds,
names: params.BucketNames,
},
{
kind: api.TEMPLATEKIND_CHECK,
ids: params.CheckIds,
names: params.CheckNames,
},
{
kind: api.TEMPLATEKIND_DASHBOARD,
ids: params.DashboardIds,
names: params.DashboardNames,
},
{
kind: api.TEMPLATEKIND_LABEL,
ids: params.LabelIds,
names: params.LabelNames,
},
{
kind: api.TEMPLATEKIND_NOTIFICATION_ENDPOINT,
ids: params.EndpointIds,
names: params.EndpointNames,
},
{
kind: api.TEMPLATEKIND_NOTIFICATION_RULE,
ids: params.RuleIds,
names: params.RuleNames,
},
{
kind: api.TEMPLATEKIND_TASK,
ids: params.TaskIds,
names: params.TaskNames,
},
{
kind: api.TEMPLATEKIND_TELEGRAF,
ids: params.TelegrafIds,
names: params.TelegrafNames,
},
{
kind: api.TEMPLATEKIND_VARIABLE,
ids: params.VariableIds,
names: params.VariableNames,
},
}
for _, filter := range filters {
for _, id := range filter.ids {
for typ, ids := range params.IdsPerType {
for _, id := range ids {
id := id
exportReq.Resources = append(exportReq.Resources, api.TemplateExportResources{
Kind: filter.kind,
Kind: typ,
Id: &id,
})
}
for _, name := range filter.names {
}
for typ, names := range params.NamesPerType {
for _, name := range names {
name := name
exportReq.Resources = append(exportReq.Resources, api.TemplateExportResources{
Kind: filter.kind,
Kind: typ,
Name: &name,
})
}
@ -131,7 +65,7 @@ type AllParams struct {
OrgName string
LabelFilters []string
KindFilters []ResourceType
KindFilters []string
}
func (c Client) ExportAll(ctx context.Context, params *AllParams) error {
@ -162,32 +96,7 @@ func (c Client) ExportAll(ctx context.Context, params *AllParams) error {
orgExport.ResourceFilters.ByLabel = &params.LabelFilters
}
if len(params.KindFilters) > 0 {
kinds := make([]api.TemplateKind, len(params.KindFilters))
for i, kf := range params.KindFilters {
switch kf {
case TypeBucket:
kinds[i] = api.TEMPLATEKIND_BUCKET
case TypeCheck:
kinds[i] = api.TEMPLATEKIND_CHECK
case TypeDashboard:
kinds[i] = api.TEMPLATEKIND_DASHBOARD
case TypeLabel:
kinds[i] = api.TEMPLATEKIND_LABEL
case TypeNotificationEndpoint:
kinds[i] = api.TEMPLATEKIND_NOTIFICATION_ENDPOINT
case TypeNotificationRule:
kinds[i] = api.TEMPLATEKIND_NOTIFICATION_RULE
case TypeTask:
kinds[i] = api.TEMPLATEKIND_TASK
case TypeTelegraf:
kinds[i] = api.TEMPLATEKIND_TELEGRAF
case TypeVariable:
kinds[i] = api.TEMPLATEKIND_VARIABLE
default:
return fmt.Errorf("unsupported resourceKind filter %q", kf)
}
}
orgExport.ResourceFilters.ByResourceKind = &kinds
orgExport.ResourceFilters.ByResourceKind = &params.KindFilters
}
}

View File

@ -1,99 +0,0 @@
package export
import (
"fmt"
"strings"
)
type ResourceType int
const (
TypeUnset ResourceType = iota
TypeBucket
TypeCheck
TypeCheckDeadman
TypeCheckThreshold
TypeDashboard
TypeLabel
TypeNotificationEndpoint
TypeNotificationEndpointHTTP
TypeNotificationEndpointPagerDuty
TypeNotificationEndpointSlack
TypeNotificationRule
TypeTask
TypeTelegraf
TypeVariable
)
func (r ResourceType) String() string {
switch r {
case TypeBucket:
return "bucket"
case TypeCheck:
return "check"
case TypeCheckDeadman:
return "checkDeadman"
case TypeCheckThreshold:
return "checkThreshold"
case TypeDashboard:
return "dashboard"
case TypeLabel:
return "label"
case TypeNotificationEndpoint:
return "notificationEndpoint"
case TypeNotificationEndpointHTTP:
return "notificationEndpointHTTP"
case TypeNotificationEndpointPagerDuty:
return "notificationEndpointPagerDuty"
case TypeNotificationEndpointSlack:
return "notificationEndpointSlack"
case TypeNotificationRule:
return "notificationRule"
case TypeTask:
return "task"
case TypeTelegraf:
return "telegraf"
case TypeVariable:
return "variable"
case TypeUnset:
fallthrough
default:
return "unset"
}
}
func (r *ResourceType) Set(v string) error {
switch strings.ToLower(v) {
case "bucket":
*r = TypeBucket
case "check":
*r = TypeCheck
case "checkdeadman":
*r = TypeCheckDeadman
case "checkthreshold":
*r = TypeCheckThreshold
case "dashboard":
*r = TypeDashboard
case "label":
*r = TypeLabel
case "notificationendpoint":
*r = TypeNotificationEndpoint
case "notificationendpointhttp":
*r = TypeNotificationEndpointHTTP
case "notificationendpointpagerduty":
*r = TypeNotificationEndpointPagerDuty
case "notificationendpointslack":
*r = TypeNotificationEndpointSlack
case "notificationrule":
*r = TypeNotificationRule
case "task":
*r = TypeTask
case "telegraf":
*r = TypeTelegraf
case "variable":
*r = TypeVariable
default:
return fmt.Errorf("unknown resource type: %s", v)
}
return nil
}