Skip to content

Commit

Permalink
Merge pull request #694 from ystia/feature/GH-687-openstack-compute-m…
Browse files Browse the repository at this point in the history
…etadata

Feature/gh 687 openstack compute metadata
  • Loading branch information
laurentganne committed Oct 13, 2020
2 parents 38e70ce + 6a5120a commit 836ca4f
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

### ENHANCEMENTS

* Add the ability to define OpenStack Compute Instance metadata ([GH-687](https://github.com/ystia/yorc/issues/687))
* Support Alien4Cloud 3.0 ([GH-689](https://github.com/ystia/yorc/issues/689))
* Upgrade Ansible version from 2.7.9 to 2.10.0 ([GH-648](https://github.com/ystia/yorc/issues/648))
* Alien4Cloud download URL change ([GH-637](https://github.com/ystia/yorc/issues/637))
Expand Down
8 changes: 7 additions & 1 deletion data/tosca/yorc-openstack-types.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ tosca_definitions_version: yorc_tosca_simple_yaml_1_0
metadata:
template_name: yorc-openstack-types
template_author: yorc
template_version: 1.2.0
template_version: 1.2.1

imports:
- yorc: <yorc-types.yml>
Expand Down Expand Up @@ -134,6 +134,12 @@ node_types:
description: >
Comma-separated list of security groups to add to the Compute
required: false
metadata:
type: map
description: Metadata key/value pairs to make available from within the instance
entry_schema:
type: string
required: false
requirements:
- group:
capability: yorc.capabilities.Group
Expand Down
16 changes: 15 additions & 1 deletion prov/terraform/openstack/osinstance.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ package openstack

import (
"context"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"path"
"strconv"
"strings"

"github.com/pkg/errors"

"github.com/ystia/yorc/v4/config"
"github.com/ystia/yorc/v4/deployments"
"github.com/ystia/yorc/v4/helper/consulutil"
Expand Down Expand Up @@ -163,6 +165,18 @@ func generateComputeInstance(ctx context.Context, opts osInstanceOptions) (Compu
}
}

toscaVal, err := deployments.GetNodePropertyValue(ctx, opts.deploymentID, opts.nodeName, "metadata")
if err != nil {
return instance, err
}
if toscaVal != nil && toscaVal.RawString() != "" {
err = json.Unmarshal([]byte(toscaVal.RawString()), &instance.Metadata)
if err != nil {
err = errors.Wrapf(err, "Expected a map of strings for the metadata value of node %s instance %s, got: %s",
opts.nodeName, opts.instanceName, toscaVal.RawString())
}
}

return instance, err
}

Expand Down
10 changes: 7 additions & 3 deletions prov/terraform/openstack/osinstance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ package openstack

import (
"context"
"github.com/ystia/yorc/v4/storage"
"github.com/ystia/yorc/v4/storage/types"
"github.com/ystia/yorc/v4/tosca"
"path"
"testing"
"time"

"github.com/ystia/yorc/v4/storage"
"github.com/ystia/yorc/v4/storage/types"
"github.com/ystia/yorc/v4/tosca"

"github.com/hashicorp/consul/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -93,6 +94,9 @@ func testSimpleOSInstance(t *testing.T) {
require.Len(t, compute.SecurityGroups, 2)
require.Contains(t, compute.SecurityGroups, "default")
require.Contains(t, compute.SecurityGroups, "openbar")
require.Len(t, compute.Metadata, 2)
require.Equal(t, "firstValue", compute.Metadata["firstKey"])
require.Equal(t, "secondValue", compute.Metadata["secondKey"])

require.Len(t, compute.Provisioners, 0)
require.Contains(t, infrastructure.Resource, "null_resource")
Expand Down
28 changes: 14 additions & 14 deletions prov/terraform/openstack/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@ const (

// A ComputeInstance represent an OpenStack compute
type ComputeInstance struct {
Region string `json:"region"`
Name string `json:"name,omitempty"`
ImageID string `json:"image_id,omitempty"`
ImageName string `json:"image_name,omitempty"`
BootVolume *BootVolume `json:"block_device,omitempty"`
FlavorID string `json:"flavor_id,omitempty"`
FlavorName string `json:"flavor_name,omitempty"`
FloatingIP string `json:"floating_ip,omitempty"`
SecurityGroups []string `json:"security_groups,omitempty"`
AvailabilityZone string `json:"availability_zone,omitempty"`
Networks []ComputeNetwork `json:"network,omitempty"`
KeyPair string `json:"key_pair,omitempty"`
SchedulerHints SchedulerHints `json:"scheduler_hints,omitempty"`

Region string `json:"region"`
Name string `json:"name,omitempty"`
ImageID string `json:"image_id,omitempty"`
ImageName string `json:"image_name,omitempty"`
BootVolume *BootVolume `json:"block_device,omitempty"`
FlavorID string `json:"flavor_id,omitempty"`
FlavorName string `json:"flavor_name,omitempty"`
FloatingIP string `json:"floating_ip,omitempty"`
SecurityGroups []string `json:"security_groups,omitempty"`
AvailabilityZone string `json:"availability_zone,omitempty"`
Networks []ComputeNetwork `json:"network,omitempty"`
KeyPair string `json:"key_pair,omitempty"`
SchedulerHints SchedulerHints `json:"scheduler_hints,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
commons.Resource
}

Expand Down
8 changes: 8 additions & 0 deletions prov/terraform/openstack/testdata/simpleOSInstance.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ imports:
- openstack-types: <yorc-openstack-types.yml>

topology_template:
inputs:
vm_metadata:
type: map
entry_schema:
type: string
default: {"firstKey": "firstValue", "secondKey": "secondValue"}
required: false
node_templates:
Compute:
type: yorc.nodes.openstack.Compute
Expand All @@ -18,6 +25,7 @@ topology_template:
availability_zone: nova
key_pair: yorc
security_groups: openbar,default
metadata: {get_input: vm_metadata}
capabilities:
endpoint:
properties:
Expand Down

0 comments on commit 836ca4f

Please sign in to comment.