Skip to content

Commit

Permalink
Using array of locations instead of maps to avoid viper lower case issue
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentganne committed Sep 25, 2019
1 parent 528da35 commit 27fbac2
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 96 deletions.
36 changes: 19 additions & 17 deletions commands/bootstrap/inputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,9 +416,9 @@ func initializeInputs(inputFilePath, resourcesPath string, configuration config.
// if one and only one Yorc location is already defined in inputs,
// selecting this infrastructure
if len(inputValues.Locations) == 1 && len(inputValues.Hosts) == 0 {
for k, v := range inputValues.Locations {
infrastructureType = v.Type
locationName = k
for _, locationConfig := range inputValues.Locations {
infrastructureType = locationConfig.Type
locationName = locationConfig.Name
}
} else if len(inputValues.Locations) == 0 && len(inputValues.Hosts) > 0 {
infrastructureType = "hostspool"
Expand Down Expand Up @@ -611,21 +611,23 @@ func initializeInputs(inputFilePath, resourcesPath string, configuration config.

askIfNotRequired := false
convertBooleanToString := false
var locationConfig config.LocationConfiguration
// Get infrastructure inputs, except in the Hosts Pool case as Hosts Pool
// doesn't have any infrastructure property
if infrastructureType != "hostspool" {
if inputValues.Locations == nil {
askIfNotRequired = true
inputValues.Locations = make(map[string]config.LocationConfiguration)

for _, v := range inputValues.Locations {
if v.Name == locationName {
locationConfig = v
break
}
}

locationConfig, ok := inputValues.Locations[locationName]
if !ok {
if locationConfig.Name == "" {
askIfNotRequired = true
locationConfig = config.LocationConfiguration{
Type: infrastructureType,
}
inputValues.Locations[locationName] = locationConfig
locationConfig.Name = locationName
locationConfig.Type = infrastructureType
inputValues.Locations = append(inputValues.Locations, locationConfig)
}

props := locationConfig.Properties
Expand Down Expand Up @@ -773,7 +775,7 @@ func initializeInputs(inputFilePath, resourcesPath string, configuration config.
inputValues.Location.Name = locationName

// Properties refedenced in go template files used to build the topology
inputValues.Location.Properties = inputValues.Locations[locationName].Properties
inputValues.Location.Properties = locationConfig.Properties

if reviewInputs {
if err := reviewAndUpdateInputs(); err != nil {
Expand All @@ -783,15 +785,15 @@ func initializeInputs(inputFilePath, resourcesPath string, configuration config.

// Post treatment needed on Google Cloud
if infrastructureType == "google" {
if err := prepareGoogleInfraInputs(locationName); err != nil {
if err := prepareGoogleInfraInputs(locationConfig); err != nil {
return err
}
}

// In insecure mode, the infrastructure secrets will not be stored in vault
// (Hosts Pool infrastructure config doesn't have this use_vault property)
if insecure && infrastructureType != "hostspool" {
inputValues.Locations[locationName].Properties.Set("use_vault", false)
locationConfig.Properties.Set("use_vault", false)
}

exportInputs()
Expand Down Expand Up @@ -1465,9 +1467,9 @@ func isDatatype(topology tosca.Topology, nodeType string) bool {

// prepareGoogleInfraInputs updates inputs for a Google Infrastructure if needed
// to use the content of service account key file instead of the path to this file
func prepareGoogleInfraInputs(locationName string) error {
func prepareGoogleInfraInputs(locationConfig config.LocationConfiguration) error {

props := inputValues.Locations[locationName].Properties
props := locationConfig.Properties
if !props.IsSet("application_credentials") {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion commands/bootstrap/testdata/inputs_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ yorc:
yorc_plugin:
download_url: https://ystia.jfrog.io/ystia/binaries/ystia/yorc-a4c-plugin/dist/develop/alien4cloud-yorc-plugin-4.0.0-SNAPSHOT.zip
locations:
myLocation:
- name: myLocation
type: openstack
properties:
auth_url: http://1.2.3.5:5000/v2.0
Expand Down
2 changes: 1 addition & 1 deletion commands/bootstrap/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ type TopologyValues struct {
Consul ConsulConfiguration
Terraform TerraformConfiguration
Yorc YorcConfiguration
Locations map[string]config.LocationConfiguration
Locations []config.LocationConfiguration
Compute config.DynamicMap
Credentials *CredentialsConfiguration
Address config.DynamicMap
Expand Down
5 changes: 1 addition & 4 deletions commands/bootstrap/topology_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"os"
"path"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -51,9 +50,7 @@ func TestCreateTopology(t *testing.T) {
err = initializeInputs("testdata/inputs_test.yaml", resourcesDir, configuration)
require.NoError(t, err, "Failed to initialize inputs")

// For the moment, viper when reading the configuration is lowercasing Map keys.
// So a location name will be lowercased by viper
assert.Equal(t, strings.ToLower("myLocation"), inputValues.Location.Name, "Unexpected location name in input values %+v", inputValues)
assert.Equal(t, "myLocation", inputValues.Location.Name, "Unexpected location name in input values %+v", inputValues)

err = createTopology(topologyDir)
require.NoError(t, err, "Failed to create topology")
Expand Down
2 changes: 1 addition & 1 deletion commands/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ func GetConfig() config.Configuration {
}

if configuration.Locations == nil {
configuration.Locations = make(map[string]config.LocationConfiguration)
configuration.Locations = make([]config.LocationConfiguration, 1)
}
if configuration.Vault == nil {
configuration.Vault = make(config.DynamicMap)
Expand Down
14 changes: 7 additions & 7 deletions commands/testdata/config_flat.yorc.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@
"tls_skip_verify": false,
"token": "blabla"
},
"locations":{
"location1":{
"locations": [
{ "name": "location1",
"type": "openstack",
"properties": {
"auth_url": "http://openstack:5000/v2.0",
"auth_url": "http://openstack:5000/v2.0",
"tenant_name": "Tname",
"tenant_id": "use_tid_or_tname",
"user_name": "{{with (secret \"/secret/yorc/mysecret\").Raw}}{{.Data.value}}{{end}}",
Expand All @@ -57,7 +57,7 @@
"os_default_security_groups": ["default", "lax"]
}
},
"location2":{
{ "name": "location2",
"type": "kubernetes",
"properties": {
"master_url": "https://kube:6443",
Expand All @@ -66,17 +66,17 @@
"ca_file": "/etc/pki/yorc/k8s-ca.crt"
}
},
"location3":{
{ "name": "location3",
"type": "aws",
"properties": {
"region": "us-east-2"
}
},
"location4":{
{ "name": "location4",
"type": "slurm",
"properties": {
"default_job_name": "xBD"
}
}
}
]
}
12 changes: 6 additions & 6 deletions commands/testdata/config_structured.yorc.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
"tls_skip_verify": false,
"token": "blabla"
},
"locations":{
"location1":{
"locations":[
{ "name": "location1",
"type": "openstack",
"properties": {
"auth_url": "http://openstack:5000/v2.0",
Expand All @@ -61,7 +61,7 @@
"os_default_security_groups": ["default", "lax"]
}
},
"location2":{
{ "name": "location2",
"type": "kubernetes",
"properties": {
"master_url": "https://kube:6443",
Expand All @@ -70,17 +70,17 @@
"ca_file": "/etc/pki/yorc/k8s-ca.crt"
}
},
"location3":{
{ "name": "location3",
"type": "aws",
"properties": {
"region": "us-east-2"
}
},
"location4":{
{ "name": "location4",
"type": "slurm",
"properties": {
"default_job_name": "xBD"
}
}
}
]
}
12 changes: 6 additions & 6 deletions config.yorc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"http_address": "0.0.0.0",
"resources_prefix": "yorc-",
"disable_ssh_agent": false,
"locations":{
"location1":{
"locations": [
{"name": "location1",
"type": "openstack",
"properties": {
"auth_url": "http://openstack:5000/v2.0",
Expand All @@ -23,7 +23,7 @@
"os_default_security_groups": ["default", "lax"]
}
},
"location2":{
{"name": "location2",
"type": "kubernetes",
"properties": {
"master_url": "https://kube:6443",
Expand All @@ -32,13 +32,13 @@
"ca_file": "/etc/pki/yorc/k8s-ca.crt"
}
},
"location3":{
{"name": "location3",
"type": "aws",
"properties": {
"region": "us-east-2"
}
},
"location4":{
{"name": "location4",
"type": "slurm",
"properties": {
"user_name": "benoists",
Expand All @@ -49,7 +49,7 @@
"default_job_name": "SBE"
}
}
},
],
"ansible": {
"use_openssh": true,
"cache_facts": true,
Expand Down
45 changes: 23 additions & 22 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,32 +74,33 @@ const DefaultAnsibleJobMonInterval = 15 * time.Second

// Configuration holds config information filled by Cobra and Viper (see commands package for more information)
type Configuration struct {
Ansible Ansible `yaml:"ansible,omitempty" mapstructure:"ansible"`
PluginsDirectory string `yaml:"plugins_directory,omitempty" mapstructure:"plugins_directory"`
WorkingDirectory string `yaml:"working_directory,omitempty" mapstructure:"working_directory"`
WorkersNumber int `yaml:"workers_number,omitempty" mapstructure:"workers_number"`
ServerGracefulShutdownTimeout time.Duration `yaml:"server_graceful_shutdown_timeout,omitempty" mapstructure:"server_graceful_shutdown_timeout"`
HTTPPort int `yaml:"http_port,omitempty" mapstructure:"http_port"`
HTTPAddress string `yaml:"http_address,omitempty" mapstructure:"http_address"`
KeyFile string `yaml:"key_file,omitempty" mapstructure:"key_file"`
CertFile string `yaml:"cert_file,omitempty" mapstructure:"cert_file"`
CAFile string `yaml:"ca_file,omitempty" mapstructure:"ca_file"`
CAPath string `yaml:"ca_path,omitempty" mapstructure:"ca_path"`
SSLVerify bool `yaml:"ssl_verify,omitempty" mapstructure:"ssl_verify"`
ResourcesPrefix string `yaml:"resources_prefix,omitempty" mapstructure:"resources_prefix"`
Consul Consul `yaml:"consul,omitempty" mapstructure:"consul"`
Telemetry Telemetry `yaml:"telemetry,omitempty" mapstructure:"telemetry"`
Locations map[string]LocationConfiguration `yaml:"locations,omitempty" mapstructure:"locations"`
Vault DynamicMap `yaml:"vault,omitempty" mapstructure:"vault"`
WfStepGracefulTerminationTimeout time.Duration `yaml:"wf_step_graceful_termination_timeout,omitempty" mapstructure:"wf_step_graceful_termination_timeout"`
PurgedDeploymentsEvictionTimeout time.Duration `yaml:"purged_deployments_eviction_timeout,omitempty" mapstructure:"purged_deployments_eviction_timeout"`
ServerID string `yaml:"server_id,omitempty" mapstructure:"server_id"`
Terraform Terraform `yaml:"terraform,omitempty" mapstructure:"terraform"`
DisableSSHAgent bool `yaml:"disable_ssh_agent,omitempty" mapstructure:"disable_ssh_agent"`
Ansible Ansible `yaml:"ansible,omitempty" mapstructure:"ansible"`
PluginsDirectory string `yaml:"plugins_directory,omitempty" mapstructure:"plugins_directory"`
WorkingDirectory string `yaml:"working_directory,omitempty" mapstructure:"working_directory"`
WorkersNumber int `yaml:"workers_number,omitempty" mapstructure:"workers_number"`
ServerGracefulShutdownTimeout time.Duration `yaml:"server_graceful_shutdown_timeout,omitempty" mapstructure:"server_graceful_shutdown_timeout"`
HTTPPort int `yaml:"http_port,omitempty" mapstructure:"http_port"`
HTTPAddress string `yaml:"http_address,omitempty" mapstructure:"http_address"`
KeyFile string `yaml:"key_file,omitempty" mapstructure:"key_file"`
CertFile string `yaml:"cert_file,omitempty" mapstructure:"cert_file"`
CAFile string `yaml:"ca_file,omitempty" mapstructure:"ca_file"`
CAPath string `yaml:"ca_path,omitempty" mapstructure:"ca_path"`
SSLVerify bool `yaml:"ssl_verify,omitempty" mapstructure:"ssl_verify"`
ResourcesPrefix string `yaml:"resources_prefix,omitempty" mapstructure:"resources_prefix"`
Consul Consul `yaml:"consul,omitempty" mapstructure:"consul"`
Telemetry Telemetry `yaml:"telemetry,omitempty" mapstructure:"telemetry"`
Locations []LocationConfiguration `yaml:"locations,omitempty" mapstructure:"locations"`
Vault DynamicMap `yaml:"vault,omitempty" mapstructure:"vault"`
WfStepGracefulTerminationTimeout time.Duration `yaml:"wf_step_graceful_termination_timeout,omitempty" mapstructure:"wf_step_graceful_termination_timeout"`
PurgedDeploymentsEvictionTimeout time.Duration `yaml:"purged_deployments_eviction_timeout,omitempty" mapstructure:"purged_deployments_eviction_timeout"`
ServerID string `yaml:"server_id,omitempty" mapstructure:"server_id"`
Terraform Terraform `yaml:"terraform,omitempty" mapstructure:"terraform"`
DisableSSHAgent bool `yaml:"disable_ssh_agent,omitempty" mapstructure:"disable_ssh_agent"`
}

// LocationConfiguration holds a location configuration
type LocationConfiguration struct {
Name string `yaml:"name" mapstructure:"name"`
Type string `yaml:"type,omitempty" mapstructure:"type"` // not an enum as it could be extended by plugins
Properties DynamicMap `yaml:"properties,omitempty" mapstructure:"properties"`
}
Expand Down
Loading

0 comments on commit 27fbac2

Please sign in to comment.