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
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 15 additions & 1 deletion examples/ethernet_networks/data_source.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,22 @@ provider "oneview" {
}

# Testing data source
# Gets all ethernet networks if name not given
data "oneview_ethernet_network" "ethernetnetworks" {
name = "TestEthNetwork_terraform_Rename"
}

output "oneview_ethernet_network_value" {
value = data.oneview_ethernet_network.ethernetnetworks
}

# Gets a network with name Test
data "oneview_ethernet_network" "ethernetnetworkbyname" {
name = "Test"
}


output "oneview_ethernet_network_by_name_value" {
value = data.oneview_ethernet_network.ethernetnetworkbyname.members[0]
}

resource "oneview_ethernet_network" "ethernetnetwork_1" {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/HewlettPackard/terraform-provider-oneview
go 1.15

require (
github.com/HewlettPackard/oneview-golang v6.0.0+incompatible
github.com/HewlettPackard/oneview-golang v6.1.1-0.20210525113815-98df2c8566ee+incompatible
github.com/apparentlymart/go-cidr v1.1.0 // indirect
github.com/docker/machine v0.16.2 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/HewlettPackard/oneview-golang v6.0.0+incompatible h1:QKxGpu+Mg09mJwd/X1mb6495h9AT8RAzeivk6SKe8kI=
github.com/HewlettPackard/oneview-golang v6.0.0+incompatible/go.mod h1:GJcjWgNHrKtt2lUl4xcaV3NRiuBlG138DNrFygXj4JE=
github.com/HewlettPackard/oneview-golang v6.1.1-0.20210525113815-98df2c8566ee+incompatible h1:oX+M5aRCpnpDKuBCVEz2oak9vHA4ilKo1ZCg7Fodv5s=
github.com/HewlettPackard/oneview-golang v6.1.1-0.20210525113815-98df2c8566ee+incompatible/go.mod h1:GJcjWgNHrKtt2lUl4xcaV3NRiuBlG138DNrFygXj4JE=
github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/agext/levenshtein v1.2.2 h1:0S/Yg6LYmFJ5stwQeRp6EeOcCbj7xiqQSdNelsXvaqE=
github.com/agext/levenshtein v1.2.2/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
Expand Down
99 changes: 99 additions & 0 deletions oneview/common_schema/ethernet_schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package common_schema

Choose a reason for hiding this comment

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

don't use an underscore in package name


import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func EthernetSchema() *schema.Resource {

Choose a reason for hiding this comment

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

exported function EthernetSchema should have comment or be unexported

return &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
},
"vlan_id": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
"purpose": {
Type: schema.TypeString,
Optional: true,
Default: "General",
},
"private_network": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"smart_link": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"ethernet_network_type": {
Type: schema.TypeString,
Optional: true,
Default: "Tagged",
},
"type": {
Type: schema.TypeString,
Optional: true,
Default: "ethernet-networkV3",
},
"connection_template_uri": {
Type: schema.TypeString,
Computed: true,
},
"created": {
Type: schema.TypeString,
Computed: true,
},
"modified": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Optional: 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": {
Optional: true,
Type: schema.TypeString,
Computed: true,
},
"initial_scope_uris": {
Optional: true,
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Set: schema.HashString,
},
},
}
}
169 changes: 71 additions & 98 deletions oneview/data_source_ethernet_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,92 +13,24 @@ package oneview

import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/HewlettPackard/terraform-provider-oneview/oneview/common_schema"
)

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,
Computed: true,
Optional: true,
},
"category": {
Type: schema.TypeString,
Computed: true,
},
"state": {
Type: schema.TypeString,
Computed: true,
},
"fabric_uri": {
Type: schema.TypeString,
"members": {
Type: schema.TypeList,
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: common_schema.EthernetSchema().Schema,
},
Set: schema.HashString,
},
},
}
Expand All @@ -107,29 +39,70 @@ func dataSourceEthernetNetwork() *schema.Resource {
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() {
d.SetId("")

if len(name) > 0 {
eNet, err := config.ovClient.GetEthernetNetworkByName(name)
if err != nil {
d.SetId("")
return nil
}
members := make([]map[string]interface{}, 0, 1)
members = append(members, map[string]interface{}{
"name": eNet.Name,
"purpose": eNet.Purpose,
"vlan_id": eNet.VlanId,
"smart_link": eNet.SmartLink,
"private_network": eNet.PrivateNetwork,
"ethernet_network_type": eNet.EthernetNetworkType,
"type": eNet.Type,
"created": eNet.Created,
"modified": eNet.Modified,
"uri": eNet.URI.String(),
"connection_template_uri": eNet.ConnectionTemplateUri,
"state": eNet.State,
"status": eNet.Status,
"category": eNet.Category,
"fabric_uri": eNet.FabricUri,
"etag": eNet.ETAG,
"scopesuri": eNet.ScopesUri,
})
d.Set("members", members)
d.Set("name", eNet.Name)
d.SetId(name)
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

eNetList, err := config.ovClient.GetEthernetNetworks("", "", "", "")

if err != nil {
d.SetId("")
return nil
}
members := make([]map[string]interface{}, 0, len(eNetList.Members))
for _, eNet := range eNetList.Members {
members = append(members, map[string]interface{}{
"name": eNet.Name,
"purpose": eNet.Purpose,
"vlan_id": eNet.VlanId,
"smart_link": eNet.SmartLink,
"private_network": eNet.PrivateNetwork,
"ethernet_network_type": eNet.EthernetNetworkType,
"type": eNet.Type,
"created": eNet.Created,
"modified": eNet.Modified,
"uri": eNet.URI.String(),
"connection_template_uri": eNet.ConnectionTemplateUri,
"state": eNet.State,
"status": eNet.Status,
"category": eNet.Category,
"fabric_uri": eNet.FabricUri,
"etag": eNet.ETAG,
"scopesuri": eNet.ScopesUri,
})
}
d.Set("members", members)
d.Set("name", "EthernetList")
d.SetId(string(eNetList.Members[0].URI))
return nil
}
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
}
Loading