func ParseUserData(contents string) (interface{}, error) { if len(contents) == 0 { return nil, nil } switch { case config.IsScript(contents): log.Printf("Parsing user-data as script") return config.NewScript(contents) case config.IsCloudConfig(contents): log.Printf("Parsing user-data as cloud-config") cc, err := config.NewCloudConfig(contents) if err != nil { return nil, err } if err := cc.Decode(); err != nil { return nil, err } return cc, nil case config.IsIgnitionConfig(contents): return nil, ErrIgnitionConfig default: return nil, errors.New("Unrecognized user-data format") } }
// Validate runs a series of validation tests against the given userdata and // returns a report detailing all of the issues. Presently, only cloud-configs // can be validated. func Validate(userdataBytes []byte) (Report, error) { switch { case len(userdataBytes) == 0: return Report{}, nil case config.IsScript(string(userdataBytes)): return Report{}, nil case config.IsIgnitionConfig(string(userdataBytes)): return Report{}, nil case config.IsCloudConfig(string(userdataBytes)): return validateCloudConfig(userdataBytes, Rules) default: return Report{entries: []Entry{ Entry{kind: entryError, message: `must be "#cloud-config" or begin with "#!"`, line: 1}, }}, nil } }