func parseProject(result *File, obj *hclobj.Object) error { if obj.Len() > 1 { return fmt.Errorf("only one 'project' block allowed") } // Check for invalid keys valid := []string{"name", "infrastructure"} if err := checkHCLKeys(obj, valid); err != nil { return multierror.Prefix(err, "project:") } var m map[string]interface{} if err := hcl.DecodeObject(&m, obj); err != nil { return err } // Parse the project var proj Project result.Project = &proj if err := mapstructure.WeakDecode(m, &proj); err != nil { return err } return nil }
func parseUpdate(result *structs.UpdateStrategy, obj *hclobj.Object) error { if obj.Len() > 1 { return fmt.Errorf("only one 'update' block allowed per job") } for _, o := range obj.Elem(false) { var m map[string]interface{} if err := hcl.DecodeObject(&m, o); err != nil { return err } for _, key := range []string{"stagger", "Stagger"} { if raw, ok := m[key]; ok { switch v := raw.(type) { case string: dur, err := time.ParseDuration(v) if err != nil { return fmt.Errorf("invalid stagger time '%s'", raw) } m[key] = dur case int: m[key] = time.Duration(v) * time.Second default: return fmt.Errorf("invalid type for stagger time '%s'", raw) } } } if err := mapstructure.WeakDecode(m, result); err != nil { return err } } return nil }
func parseDetect(result *Config, obj *hclobj.Object) error { // 从map中获取所有实际对象的key值 objects := make([]*hclobj.Object, 0, 2) for _, o1 := range obj.Elem(false) { for _, o2 := range o1.Elem(true) { objects = append(objects, o2) } } if len(objects) == 0 { return nil } // 检查每个对象,返回实际结果 collection := make([]*Detector, 0, len(objects)) for _, o := range objects { var m map[string]interface{} if err := hcl.DecodeObject(&m, o); err != nil { return err } var d Detector if err := mapstructure.WeakDecode(m, &d); err != nil { return fmt.Errorf("解析detector错误 '%s' : %s", o.Key, err) } d.Type = o.Key collection = append(collection, &d) } result.Detectors = collection return nil }
func parseUpdate(result *structs.UpdateStrategy, obj *hclobj.Object) error { if obj.Len() > 1 { return fmt.Errorf("only one 'update' block allowed per job") } for _, o := range obj.Elem(false) { var m map[string]interface{} if err := hcl.DecodeObject(&m, o); err != nil { return err } for _, key := range []string{"stagger", "Stagger"} { if raw, ok := m[key]; ok { staggerTime, err := toDuration(raw) if err != nil { return fmt.Errorf("Invalid stagger time: %v", err) } m[key] = staggerTime } } if err := mapstructure.WeakDecode(m, result); err != nil { return err } } return nil }
func parseDetect(result *Config, obj *hclobj.Object) error { // Get all the maps of keys to the actual object objects := make([]*hclobj.Object, 0, 2) for _, o1 := range obj.Elem(false) { for _, o2 := range o1.Elem(true) { objects = append(objects, o2) } } if len(objects) == 0 { return nil } // Go through each object and turn it into an actual result. collection := make([]*Detector, 0, len(objects)) for _, o := range objects { var m map[string]interface{} if err := hcl.DecodeObject(&m, o); err != nil { return err } var d Detector if err := mapstructure.WeakDecode(m, &d); err != nil { return fmt.Errorf( "error parsing detector '%s': %s", o.Key, err) } d.Type = o.Key collection = append(collection, &d) } result.Detectors = collection return nil }
func parseRestartPolicy(result *structs.RestartPolicy, obj *hclobj.Object) error { var restartHclObj *hclobj.Object var m map[string]interface{} if restartHclObj = obj.Get("restart", false); restartHclObj == nil { return nil } if err := hcl.DecodeObject(&m, restartHclObj); err != nil { return err } if delay, ok := m["delay"]; ok { d, err := toDuration(delay) if err != nil { return fmt.Errorf("Invalid Delay time in restart policy: %v", err) } result.Delay = d } if interval, ok := m["interval"]; ok { i, err := toDuration(interval) if err != nil { return fmt.Errorf("Invalid Interval time in restart policy: %v", err) } result.Interval = i } if attempts, ok := m["attempts"]; ok { a, err := toInteger(attempts) if err != nil { return fmt.Errorf("Invalid value in attempts: %v", err) } result.Attempts = a } return nil }
func parseConstraints(result *[]*structs.Constraint, obj *hclobj.Object) error { for _, o := range obj.Elem(false) { var m map[string]interface{} if err := hcl.DecodeObject(&m, o); err != nil { return err } m["LTarget"] = m["attribute"] m["RTarget"] = m["value"] m["Operand"] = m["operator"] // Default constraint to being hard if _, ok := m["hard"]; !ok { m["hard"] = true } // Build the constraint var c structs.Constraint if err := mapstructure.WeakDecode(m, &c); err != nil { return err } if c.Operand == "" { c.Operand = "=" } *result = append(*result, &c) } return nil }
func parseRawModules(objModules *hclObj.Object, errors *multierror.Error) (output []modules.ModuleSpec) { //iterate over each module for _, ms := range objModules.Elem(true) { // lets build the mod params ... rawParams := ms.Elem(true) params := params.NewModuleParams() for _, p := range rawParams { switch p.Value.(type) { case string: params[p.Key] = p.Value case int: params[p.Key] = p.Value case []*hclObj.Object: propertyValues := make([]interface{}, 0) for _, pv := range p.Elem(true) { propertyValues = append(propertyValues, pv.Value) } params[p.Key] = propertyValues } } // build the module module := modprobe.Find(ms.Key, params) output = append(output, module) } return output }
func parseInfra(result *File, obj *hclobj.Object) error { // Get all the maps of keys to the actual object objects := make(map[string]*hclobj.Object) for _, o1 := range obj.Elem(false) { for _, o2 := range o1.Elem(true) { if _, ok := objects[o2.Key]; ok { return fmt.Errorf( "infrastructure '%s' defined more than once", o2.Key) } objects[o2.Key] = o2 } } if len(objects) == 0 { return nil } // Go through each object and turn it into an actual result. collection := make([]*Infrastructure, 0, len(objects)) for n, o := range objects { // Check for invalid keys valid := []string{"name", "type", "flavor", "foundation"} if err := checkHCLKeys(o, valid); err != nil { return multierror.Prefix(err, fmt.Sprintf( "infrastructure '%s':", n)) } var m map[string]interface{} if err := hcl.DecodeObject(&m, o); err != nil { return err } var infra Infrastructure if err := mapstructure.WeakDecode(m, &infra); err != nil { return fmt.Errorf( "error parsing infrastructure '%s': %s", n, err) } infra.Name = n if infra.Type == "" { infra.Type = infra.Name } // Parse the foundations if we have any if o2 := o.Get("foundation", false); o != nil { if err := parseFoundations(&infra, o2); err != nil { return fmt.Errorf("error parsing 'foundation': %s", err) } } collection = append(collection, &infra) } result.Infrastructure = collection return nil }
func parseConstraints(result *[]*structs.Constraint, obj *hclobj.Object) error { for _, o := range obj.Elem(false) { var m map[string]interface{} if err := hcl.DecodeObject(&m, o); err != nil { return err } m["LTarget"] = m["attribute"] m["RTarget"] = m["value"] m["Operand"] = m["operator"] // Default constraint to being hard if _, ok := m["hard"]; !ok { m["hard"] = true } // If "version" is provided, set the operand // to "version" and the value to the "RTarget" if constraint, ok := m[structs.ConstraintVersion]; ok { m["Operand"] = structs.ConstraintVersion m["RTarget"] = constraint } // If "regexp" is provided, set the operand // to "regexp" and the value to the "RTarget" if constraint, ok := m[structs.ConstraintRegex]; ok { m["Operand"] = structs.ConstraintRegex m["RTarget"] = constraint } if value, ok := m[structs.ConstraintDistinctHosts]; ok { enabled, err := strconv.ParseBool(value.(string)) if err != nil { return err } // If it is not enabled, skip the constraint. if !enabled { continue } m["Operand"] = structs.ConstraintDistinctHosts } // Build the constraint var c structs.Constraint if err := mapstructure.WeakDecode(m, &c); err != nil { return err } if c.Operand == "" { c.Operand = "=" } *result = append(*result, &c) } return nil }
func parseResources(result *structs.Resources, obj *hclobj.Object) error { if obj.Len() > 1 { return fmt.Errorf("only one 'resource' block allowed per task") } for _, o := range obj.Elem(false) { var m map[string]interface{} if err := hcl.DecodeObject(&m, o); err != nil { return err } delete(m, "network") if err := mapstructure.WeakDecode(m, result); err != nil { return err } // Parse the network resources if o := o.Get("network", false); o != nil { if o.Len() > 1 { return fmt.Errorf("only one 'network' resource allowed") } var r structs.NetworkResource var m map[string]interface{} if err := hcl.DecodeObject(&m, o); err != nil { return err } if err := mapstructure.WeakDecode(m, &r); err != nil { return err } // Keep track of labels we've already seen so we can ensure there // are no collisions when we turn them into environment variables. // lowercase:NomalCase so we can get the first for the error message seenLabel := map[string]string{} for _, label := range r.DynamicPorts { if !reDynamicPorts.MatchString(label) { return errDynamicPorts } first, seen := seenLabel[strings.ToLower(label)] if seen { return fmt.Errorf("Found a port label collision: `%s` overlaps with previous `%s`", label, first) } else { seenLabel[strings.ToLower(label)] = label } } result.Networks = []*structs.NetworkResource{&r} } } return nil }
func parseRawObject(key string, hcltree *hclObj.Object, errors *multierror.Error) (apisOut []*hclObj.Object) { if apis := hcltree.Get(key, false); apis != nil { for _, api := range apis.Elem(true) { apisOut = append(apisOut, api) } } else { errors = multierror.Append(errors, fmt.Errorf("No job_store was specified in the configuration")) } return apisOut }
// LoadProvidersHcl recurses into the given HCL object and turns // it into a mapping of provider configs. func loadProvidersHcl(os *hclobj.Object) ([]*ProviderConfig, error) { var objects []*hclobj.Object // Iterate over all the "provider" blocks and get the keys along with // their raw configuration objects. We'll parse those later. for _, o1 := range os.Elem(false) { for _, o2 := range o1.Elem(true) { objects = append(objects, o2) } } if len(objects) == 0 { return nil, nil } // Go through each object and turn it into an actual result. result := make([]*ProviderConfig, 0, len(objects)) for _, o := range objects { var config map[string]interface{} if err := hcl.DecodeObject(&config, o); err != nil { return nil, err } delete(config, "alias") rawConfig, err := NewRawConfig(config) if err != nil { return nil, fmt.Errorf( "Error reading config for provider config %s: %s", o.Key, err) } // If we have an alias field, then add those in var alias string if a := o.Get("alias", false); a != nil { err := hcl.DecodeObject(&alias, a) if err != nil { return nil, fmt.Errorf( "Error reading alias for provider[%s]: %s", o.Key, err) } } result = append(result, &ProviderConfig{ Name: o.Key, Alias: alias, RawConfig: rawConfig, }) } return result, nil }
func parseRawBool(key string, hcltree *hclObj.Object, errors *multierror.Error) bool { if rawValue := hcltree.Get(key, false); rawValue != nil { if rawValue.Type != hclObj.ValueTypeBool { errors = multierror.Append(errors, fmt.Errorf("Invalid type assigned to property: %s. Expected string type, Found %s", key, rawValue.Type)) } else { return rawValue.Value.(bool) } } else { errors = multierror.Append(errors, fmt.Errorf("Property: %s not specified in the configuration", key)) } return false }
func parseRawArray(key string, object *hclObj.Object, errors *multierror.Error) []string { output := []string{} if rawValue := object.Get(key, false); rawValue != nil { switch rawValue.Type { case hclObj.ValueTypeString: output = append(output, rawValue.Value.(string)) case hclObj.ValueTypeList: for _, m := range rawValue.Elem(true) { output = append(output, m.Value.(string)) } } } return output }
func parseVersion(hcltree *hclObj.Object, errors *multierror.Error) string { if rawVersion := hcltree.Get("version", false); rawVersion != nil { if rawVersion.Len() > 1 { errors = multierror.Append(errors, fmt.Errorf("Version was specified more than once")) } else { if rawVersion.Type != hclObj.ValueTypeString { errors = multierror.Append(errors, fmt.Errorf("Version was specified as an invalid type - expected string, found %s", rawVersion.Type)) } else { return rawVersion.Value.(string) } } } else { errors = multierror.Append(errors, fmt.Errorf("No version was specified in the configuration")) } return "*** Error" }
func parseMaxExecutions(jobNode *hclObj.Object, errors *multierror.Error) string { if rawCron := jobNode.Get("max_executions", false); rawCron != nil { if rawCron.Len() > 1 { errors = multierror.Append(errors, fmt.Errorf("max_executions was specified more than once")) } else { if rawCron.Type != hclObj.ValueTypeString { errors = multierror.Append(errors, fmt.Errorf("max_executions was specified as an invalid type - expected string, found %s", rawCron.Type)) } else { return rawCron.Value.(string) } } } else { errors = multierror.Append(errors, fmt.Errorf("No max_executions was specified in the configuration")) } return "" }
func parseJobTrigger(jobNode *hclObj.Object, errors *multierror.Error) TriggerConfig { var triggerConfig TriggerConfig if t := jobNode.Get("trigger", false); t != nil { err := hcl.DecodeObject(&triggerConfig, t) if err != nil { errors = multierror.Append(errors, err) } } return triggerConfig // cron := parseCron(jobNode.Get("trigger", false), errors) // maxExecs := parseMaxExecutions(jobNode.Get("trigger", false), errors) // return TriggerConfig{ // Cron: cron, // MaxExecutions: maxExecs, // } }
func checkHCLKeys(obj *hclobj.Object, valid []string) error { validMap := make(map[string]struct{}, len(valid)) for _, v := range valid { validMap[v] = struct{}{} } var result error for _, o := range obj.Elem(true) { if _, ok := validMap[o.Key]; !ok { result = multierror.Append(result, fmt.Errorf( "invald key: %s", o.Key)) } } return result }
func parseJob(jobNode *hclObj.Object, errors *multierror.Error) JobConfig { config := JobConfig{} if jobNode.Len() > 1 { errors = multierror.Append(errors, fmt.Errorf("job was specified more than once")) } else { if jobNode.Type != hclObj.ValueTypeObject { errors = multierror.Append(errors, fmt.Errorf("job was specified as an invalid type - expected object, found %s", jobNode.Type)) } else { config.Name = jobNode.Key config.Trigger = parseJobTrigger(jobNode, errors) config.Exec = parseExec(jobNode, errors) } } return config }
func (d *decoder) decodeSlice(name string, o *hcl.Object, result reflect.Value) error { // If we have an interface, then we can address the interface, // but not the slice itself, so get the element but set the interface set := result if result.Kind() == reflect.Interface { result = result.Elem() } // Create the slice if it isn't nil resultType := result.Type() resultElemType := resultType.Elem() if result.IsNil() { resultSliceType := reflect.SliceOf(resultElemType) result = reflect.MakeSlice( resultSliceType, 0, 0) } // Determine how we're doing this expand := true switch o.Type { case hcl.ValueTypeObject: expand = false default: // Array or anything else: we expand values and take it all } i := 0 for _, o := range o.Elem(expand) { fieldName := fmt.Sprintf("%s[%d]", name, i) // Decode val := reflect.Indirect(reflect.New(resultElemType)) if err := d.decode(fieldName, o, val); err != nil { return err } // Append it onto the slice result = reflect.Append(result, val) i += 1 } set.Set(result) return nil }
func parseRawChain(hclObj *hclObj.Object, errors *multierror.Error) chain.ChainSpec { if rawChain := hclObj.Get("chain", false); rawChain != nil { // iterate over all modules for a given chain for _, mod := range rawChain.Elem(true) { modules := parseRawModules(mod, errors) return chain.NewWithModules(modules) } } return chain.New() }
func parseJobStore(hcltree *hclObj.Object, errors *multierror.Error) []string { hosts := make([]string, 0) if jobStore := hcltree.Get("job_store", false); jobStore != nil { if jobStore.Len() > 1 { errors = multierror.Append(errors, fmt.Errorf("job_store was specified more than once")) } else { if jobStore.Type != hclObj.ValueTypeList { errors = multierror.Append(errors, fmt.Errorf("job_store was specified as an invalid type - expected string, found %s", jobStore.Type)) } else { for _, host := range jobStore.Elem(true) { hosts = append(hosts, host.Value.(string)) } return hosts } } } else { errors = multierror.Append(errors, fmt.Errorf("No job_store was specified in the configuration")) } return []string{} }
func parseJobs(hcltree *hclObj.Object, errors *multierror.Error) []JobConfig { outputConfig := make([]JobConfig, 0) if rawJobs := hcltree.Get("job", false); rawJobs != nil { if rawJobs.Type != hclObj.ValueTypeObject { errors = multierror.Append(errors, fmt.Errorf("job was specified as an invalid type - expected list, found %s", rawJobs.Type)) } else { arrayJobs := rawJobs.Elem(true) for _, job := range arrayJobs { outputConfig = append(outputConfig, parseJob(job, errors)) } } } else { if len(outputConfig) == 0 { errors = multierror.Append(errors, fmt.Errorf("No job_store was specified in the configuration")) } } return outputConfig }
func parseApplication(result *File, obj *hclobj.Object) error { if obj.Len() > 1 { return fmt.Errorf("only one 'application' block allowed") } // Check for invalid keys valid := []string{"name", "type", "dependency"} if err := checkHCLKeys(obj, valid); err != nil { return multierror.Prefix(err, "application:") } var m map[string]interface{} if err := hcl.DecodeObject(&m, obj); err != nil { return err } var app Application result.Application = &app return mapstructure.WeakDecode(m, &app) }
func loadListeners(os *hclobj.Object) ([]*Listener, error) { var allNames []*hclobj.Object // Really confusing iteration. The key is the false/true parameter // of whether we're expanding or not. We first iterate over all // the "listeners" for _, o1 := range os.Elem(false) { // Iterate expand to get the list of types for _, o2 := range o1.Elem(true) { switch o2.Type { case hclobj.ValueTypeList: // This switch is for JSON, to allow them to do this: // // "tcp": [{ ... }, { ... }] // // To configure multiple listeners of the same type. for _, o3 := range o2.Elem(true) { o3.Key = o2.Key allNames = append(allNames, o3) } case hclobj.ValueTypeObject: // This is for the standard `listener "tcp" { ... }` syntax allNames = append(allNames, o2) } } } if len(allNames) == 0 { return nil, nil } // Now go over all the types and their children in order to get // all of the actual resources. result := make([]*Listener, 0, len(allNames)) for _, obj := range allNames { k := obj.Key var config map[string]string if err := hcl.DecodeObject(&config, obj); err != nil { return nil, fmt.Errorf( "Error reading config for %s: %s", k, err) } result = append(result, &Listener{ Type: k, Config: config, }) } return result, nil }
// LoadProvidersHcl recurses into the given HCL object and turns // it into a mapping of provider configs. func loadProvidersHcl(os *hclobj.Object) ([]*ProviderConfig, error) { objects := make(map[string]*hclobj.Object) // Iterate over all the "provider" blocks and get the keys along with // their raw configuration objects. We'll parse those later. for _, o1 := range os.Elem(false) { for _, o2 := range o1.Elem(true) { objects[o2.Key] = o2 } } if len(objects) == 0 { return nil, nil } // Go through each object and turn it into an actual result. result := make([]*ProviderConfig, 0, len(objects)) for n, o := range objects { var config map[string]interface{} if err := hcl.DecodeObject(&config, o); err != nil { return nil, err } rawConfig, err := NewRawConfig(config) if err != nil { return nil, fmt.Errorf( "Error reading config for provider config %s: %s", n, err) } result = append(result, &ProviderConfig{ Name: n, RawConfig: rawConfig, }) } return result, nil }
func parseImport(result *File, obj *hclobj.Object) error { // Get all the maps of keys to the actual object objects := make([]*hclobj.Object, 0, 3) set := make(map[string]struct{}) for _, o1 := range obj.Elem(false) { for _, o2 := range o1.Elem(true) { if _, ok := set[o2.Key]; ok { return fmt.Errorf( "imported '%s' more than once", o2.Key) } objects = append(objects, o2) set[o2.Key] = struct{}{} } } if len(objects) == 0 { return nil } // Go through each object and turn it into an actual result. collection := make([]*Import, 0, len(objects)) for _, o := range objects { // Check for invalid keys if err := checkHCLKeys(o, nil); err != nil { return multierror.Prefix(err, fmt.Sprintf( "import '%s':", o.Key)) } collection = append(collection, &Import{ Source: o.Key, }) } result.Imports = collection return nil }
func parseFoundations(result *Infrastructure, obj *hclobj.Object) error { // Get all the maps of keys to the actual object objects := make(map[string]*hclobj.Object) for _, o1 := range obj.Elem(false) { for _, o2 := range o1.Elem(true) { if _, ok := objects[o2.Key]; ok { return fmt.Errorf( "foundation '%s' defined more than once", o2.Key) } objects[o2.Key] = o2 } } if len(objects) == 0 { return nil } // Go through each object and turn it into an actual result. collection := make([]*Foundation, 0, len(objects)) for n, o := range objects { var m map[string]interface{} if err := hcl.DecodeObject(&m, o); err != nil { return err } var f Foundation f.Name = n f.Config = m collection = append(collection, &f) } // Set the results result.Foundations = collection return nil }
func parseCustomizations(result *File, obj *hclobj.Object) error { // Get all the maps of keys to the actual object objects := make(map[string]*hclobj.Object) for _, o1 := range obj.Elem(false) { for _, o2 := range o1.Elem(true) { if _, ok := objects[o2.Key]; ok { return fmt.Errorf( "customization '%s' defined more than once", o2.Key) } objects[o2.Key] = o2 } } if len(objects) == 0 { return nil } // Go through each object and turn it into an actual result. collection := make([]*Customization, 0, len(objects)) for n, o := range objects { var m map[string]interface{} if err := hcl.DecodeObject(&m, o); err != nil { return err } var c Customization c.Type = strings.ToLower(n) c.Config = m collection = append(collection, &c) } result.Customization = &CustomizationSet{Raw: collection} return nil }