Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support GetAll for ethernet resource #313

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
253 changes: 154 additions & 99 deletions oneview/data_source_ethernet_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,124 +12,179 @@
package oneview

import (
"github.com/HewlettPackard/oneview-golang/ov"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/HewlettPackard/oneview-golang/utils"
"encoding/json"
"io/ioutil"
"fmt"
"strconv"
"time"
)

func dataSourceEthernetNetwork() *schema.Resource {
return &schema.Resource{
Read: dataSourceEthernetNetworkRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"vlan_id": {
Type: schema.TypeInt,
Computed: true,
},
"purpose": {
Type: schema.TypeString,
Computed: true,
},
"private_network": {
Type: schema.TypeBool,
Computed: true,
},
"smart_link": {
Type: schema.TypeBool,
Computed: true,
},
"ethernet_network_type": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
"connection_template_uri": {
Type: schema.TypeString,
Computed: true,
},
"created": {
Type: schema.TypeString,
Computed: true,
},
"modified": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"uri": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
"count_": {
Type: schema.TypeInt,
Computed: true,
},
"total": {
Type: schema.TypeInt,
Computed: true,
},
"list_uri": {
Type: schema.TypeString,
Computed: true,
},
"members": {
Type: schema.TypeList,
Computed: true,
},
"category": {
Type: schema.TypeString,
Computed: true,
},
"state": {
Type: schema.TypeString,
Computed: true,
},
"fabric_uri": {
Type: schema.TypeString,
Computed: true,
},
"etag": {
Type: schema.TypeString,
Computed: true,
},
"scopesuri": {
Type: schema.TypeString,
Computed: true,
},
"initial_scope_uris": {
Computed: true,
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,

Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"vlan_id": {
Type: schema.TypeInt,
Computed: true,
},
"purpose": {
Type: schema.TypeString,
Computed: true,
},
"private_network": {
Type: schema.TypeBool,
Computed: true,
},
"smart_link": {
Type: schema.TypeBool,
Computed: true,
},
"ethernet_network_type": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
"connection_template_uri": {
Type: schema.TypeString,
Computed: true,
},
"created": {
Type: schema.TypeString,
Computed: true,
},
"modified": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"uri": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"category": {
Type: schema.TypeString,
Computed: true,
},
"state": {
Type: schema.TypeString,
Computed: true,
},
"fabric_uri": {
Type: schema.TypeString,
Computed: true,
},
"etag": {
Type: schema.TypeString,
Computed: true,
},
"scopesuri": {
Type: schema.TypeString,
Computed: true,
},
"initial_scope_uris": {
Computed: true,
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Set: schema.HashString,
},
},
},
Set: schema.HashString,
},

},

}
}

func dataSourceEthernetNetworkRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
name := d.Get("name").(string)
eNet, err := config.ovClient.GetEthernetNetworkByName(name)
if err != nil || eNet.URI.IsNil() {
eNetList, err := config.ovClient.GetEthernetNetworks("", "", "", "")

if err != nil {
d.SetId("")
return nil
} else {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if block ends with a return statement, so drop this else and outdent its block

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if block ends with a return statement, so drop this else and outdent its block

d.Set("count_", eNetList.Count)
d.Set("total", eNetList.Total)
d.Set("list_uri", eNetList.URI)

elist := [] ov.EthernetNetwork{}
for i := range eNetList.Members {
ethernet := ov.EthernetNetwork{}
ethernet.Name = eNetList.Members[i].Name
ethernet.VlanId = eNetList.Members[i].VlanId
ethernet.Purpose = eNetList.Members[i].Purpose
ethernet.SmartLink = eNetList.Members[i].SmartLink
ethernet.PrivateNetwork = eNetList.Members[i].PrivateNetwork
ethernet.EthernetNetworkType = eNetList.Members[i].EthernetNetworkType
ethernet.Type = eNetList.Members[i].Type
ethernet.Created = eNetList.Members[i].Created
ethernet.Modified = eNetList.Members[i].Modified
ethernet.URI = utils.Nstring(eNetList.Members[i].URI.String())
ethernet.ConnectionTemplateUri = utils.Nstring(eNetList.Members[i].ConnectionTemplateUri.String())
ethernet.State = eNetList.Members[i].State
ethernet.Status = eNetList.Members[i].Status
ethernet.Category = eNetList.Members[i].Category
ethernet.FabricUri = utils.Nstring(eNetList.Members[i].FabricUri.String())
ethernet.ETAG = eNetList.Members[i].ETAG
ethernet.ScopesUri = utils.Nstring(eNetList.Members[i].ScopesUri.String())

initialScopeUris := make([]utils.Nstring, len(eNetList.Members[i].InitialScopeUris))
for _, scope := range eNetList.Members[i].InitialScopeUris {
initialScopeUri := utils.Nstring(scope.String())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var initialScopeUri should be initialScopeURI

initialScopeUris = append(initialScopeUris, initialScopeUri)
}

ethernet.InitialScopeUris = initialScopeUris
elist = append(elist, ethernet)

}
file, _ := json.MarshalIndent(elist, "", " ")
file_name := fmt.Sprintf("test_check_%d.json", 5)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't use underscores in Go names; var file_name should be fileName

_ = ioutil.WriteFile(file_name, file, 0644)

d.Set("members", elist)
d.SetId(strconv.FormatInt(time.Now().Unix(), 10))

}
d.Set("name", eNet.Name)
d.Set("vlan_id", eNet.VlanId)
d.Set("purpose", eNet.Purpose)
d.Set("smart_link", eNet.SmartLink)
d.Set("private_network", eNet.PrivateNetwork)
d.Set("ethernet_network_type", eNet.EthernetNetworkType)
d.Set("type", eNet.Type)
d.Set("created", eNet.Created)
d.Set("modified", eNet.Modified)
d.Set("uri", eNet.URI.String())
d.Set("connection_template_uri", eNet.ConnectionTemplateUri.String())
d.Set("status", eNet.Status)
d.Set("category", eNet.Category)
d.Set("state", eNet.State)
d.Set("fabric_uri", eNet.FabricUri.String())
d.Set("etag", eNet.ETAG)
d.Set("scopesuri", eNet.ScopesUri.String())
d.Set("initial_scope_uris", eNet.InitialScopeUris)
d.SetId(name)
return nil

}