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

Add new data source for security groups #40

Open
wants to merge 2 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
47 changes: 47 additions & 0 deletions data_source_security_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/sky-uk/gonsx"
"github.com/sky-uk/gonsx/api/securitygroup"
)

func dataSourceSecurityGroup() *schema.Resource {

return &schema.Resource{

Read: dataSourceSecurityGroupRead,

Schema: map[string]*schema.Schema{

"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"scopeid": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "globalroot-0",
},
},
}
}

func dataSourceSecurityGroupRead(d *schema.ResourceData, meta interface{}) error {
nsxclient := meta.(*gonsx.NSXClient)
getAllAPI := securitygroup.NewGetAll(d.Get("scopeid").(string))

err := nsxclient.Do(getAllAPI)
if err != nil {
return err
}
name := d.Get("name").(string)
for _, secGroup := range getAllAPI.GetResponse().SecurityGroups {
if secGroup.Name == name {
d.SetId(secGroup.ObjectID)
return nil
}
}
return fmt.Errorf("Security group %s not found", name)
}
4 changes: 4 additions & 0 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func Provider() terraform.ResourceProvider {
"nsx_firewall_exclusion": resourceFirewallExclusion(),
},

DataSourcesMap: map[string]*schema.Resource{
"nsx_security_group": dataSourceSecurityGroup(),
},

ConfigureFunc: providerConfigure,
}
}
Expand Down