-
Notifications
You must be signed in to change notification settings - Fork 32
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 resource and datasource mso_schema_site_l3out (DCNE-149) #170
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,6 @@ | |
**/terraform.tfstate | ||
**/terraform.tfstate.backup | ||
**/.vscode | ||
**/.vscode/* | ||
**/.terraform.lock.hcl | ||
**/*.log |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
terraform { | ||
required_providers { | ||
mso = { | ||
source = "CiscoDevNet/mso" | ||
} | ||
} | ||
} | ||
|
||
provider "mso" { | ||
username = "" # <MSO username> | ||
password = "" # <MSO pwd> | ||
url = "" # <MSO URL> | ||
insecure = true | ||
} | ||
|
||
data "mso_site" "example" { | ||
name = "example" | ||
} | ||
|
||
data "mso_tenant" "example" { | ||
name = "example" | ||
display_name = "example" | ||
} | ||
|
||
resource "mso_schema" "example" { | ||
name = "example" | ||
template_name = "example" | ||
tenant_id = data.mso_tenant.example.id | ||
} | ||
|
||
resource "mso_schema_template_vrf" "vrf" { | ||
schema_id = mso_schema.example.id | ||
template = mso_schema.example.template_name | ||
name = "example" | ||
} | ||
|
||
resource "mso_schema_template_l3out" "l3out" { | ||
schema_id = mso_schema.example.id | ||
template_name = mso_schema.example.template_name | ||
l3out_name = "example" | ||
vrf_name = mso_schema_template_vrf.vrf.id | ||
vrf_schema_id = mso_schema_template_vrf.vrf.schema_id | ||
vrf_template_name = mso_schema_template_vrf.vrf.template | ||
} | ||
|
||
|
||
resource "mso_schema_site" "example" { | ||
schema_id = mso_schema.example.id | ||
site_id = data.mso_site.example.id | ||
template_name = "example" | ||
} | ||
|
||
resource "mso_schema_site_vrf" "example" { | ||
template_name = mso_schema_site.example.template_name | ||
site_id = mso_schema_site.example.site_id | ||
schema_id = mso_schema_site.example.schema_id | ||
vrf_name = mso_schema_template_vrf.example.name | ||
} | ||
|
||
resource "mso_schema_site_l3out" "example" { | ||
schema_id = mso_schema_site.example.schema_id | ||
l3out_name = mso_schema_template_l3out.l3out.l3out_name | ||
template_name = mso_schema_site.example.template_name | ||
vrf_name = mso_schema_site_vrf.example.vrf_name | ||
site_id = mso_schema_site.example.site_id | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package mso | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/ciscoecosystem/mso-go-client/client" | ||
"github.com/ciscoecosystem/mso-go-client/models" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/validation" | ||
) | ||
|
||
func datasourceMSOSchemaSiteL3out() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: datasourceMSOSchemaSiteL3outRead, | ||
SchemaVersion: 1, | ||
Schema: map[string]*schema.Schema{ | ||
"l3out_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringLenBetween(1, 1000), | ||
}, | ||
"vrf_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringLenBetween(1, 1000), | ||
}, | ||
"template_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringLenBetween(1, 1000), | ||
}, | ||
"site_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringLenBetween(1, 1000), | ||
}, | ||
"schema_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringLenBetween(1, 1000), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func datasourceMSOSchemaSiteL3outRead(d *schema.ResourceData, m interface{}) error { | ||
log.Println("[DEBUG] Schema Site L3out: Beginning Read") | ||
msoClient := m.(*client.Client) | ||
schemaId := d.Get("schema_id").(string) | ||
siteId := d.Get("site_id").(string) | ||
templateName := d.Get("template_name").(string) | ||
vrfName := d.Get("vrf_name").(string) | ||
l3outName := d.Get("l3out_name").(string) | ||
l3outMap := models.IntersiteL3outs{ | ||
SchemaID: schemaId, | ||
SiteId: siteId, | ||
TemplateName: templateName, | ||
VRFName: vrfName, | ||
L3outName: l3outName, | ||
} | ||
l3outMapRemote, err := msoClient.ReadIntersiteL3outs(&l3outMap) | ||
if err != nil { | ||
d.SetId("") | ||
return err | ||
} | ||
setMSOSchemaSiteL3outAttributes(l3outMapRemote, d) | ||
d.SetId(L3outModelToL3outId(&l3outMap)) | ||
log.Println("[DEBUG] Schema Site L3out: Reading Completed", d.Id()) | ||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package mso | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"github.com/ciscoecosystem/mso-go-client/client" | ||
"github.com/ciscoecosystem/mso-go-client/models" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/validation" | ||
) | ||
|
||
func resourceMSOSchemaSiteL3out() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceMSOSchemaSiteL3outCreate, | ||
Read: resourceMSOSchemaSiteL3outRead, | ||
Delete: resourceMSOSchemaSiteL3outDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: resourceMSOSchemaSiteL3outImport, | ||
}, | ||
SchemaVersion: 1, | ||
Schema: map[string]*schema.Schema{ | ||
"l3out_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.StringLenBetween(1, 1000), | ||
}, | ||
"vrf_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.StringLenBetween(1, 1000), | ||
}, | ||
"template_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.StringLenBetween(1, 1000), | ||
}, | ||
"site_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.StringLenBetween(1, 1000), | ||
}, | ||
"schema_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.StringLenBetween(1, 1000), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func setMSOSchemaSiteL3outAttributes(l3outMap *models.IntersiteL3outs, d *schema.ResourceData) { | ||
d.Set("l3out_name", l3outMap.L3outName) | ||
d.Set("vrf_name", l3outMap.VRFName) | ||
d.Set("template_name", l3outMap.TemplateName) | ||
d.Set("site_id", l3outMap.SiteId) | ||
d.Set("schema_id", l3outMap.SchemaID) | ||
} | ||
|
||
func resourceMSOSchemaSiteL3outImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) { | ||
log.Println("[DEBUG] Schema Site L3out: Beginning Import", d.Id()) | ||
msoClient := m.(*client.Client) | ||
id := d.Id() | ||
L3out, err := L3outIdToL3outModel(id) | ||
if err != nil { | ||
return nil, err | ||
} | ||
l3outMapRemote, err := msoClient.ReadIntersiteL3outs(L3out) | ||
if err != nil { | ||
return nil, err | ||
} | ||
setMSOSchemaSiteL3outAttributes(l3outMapRemote, d) | ||
d.SetId(id) | ||
log.Println("[DEBUG] Schema Site L3out: Import Completed", d.Id()) | ||
return []*schema.ResourceData{d}, nil | ||
} | ||
|
||
func resourceMSOSchemaSiteL3outCreate(d *schema.ResourceData, m interface{}) error { | ||
log.Printf("[DEBUG] Schema Site L3out: Beginning Creation") | ||
msoClient := m.(*client.Client) | ||
schemaId := d.Get("schema_id").(string) | ||
siteId := d.Get("site_id").(string) | ||
templateName := d.Get("template_name").(string) | ||
vrfName := d.Get("vrf_name").(string) | ||
l3outName := d.Get("l3out_name").(string) | ||
l3outMap := models.IntersiteL3outs{ | ||
L3outName: l3outName, | ||
VRFName: vrfName, | ||
SiteId: siteId, | ||
TemplateName: templateName, | ||
SchemaID: schemaId, | ||
} | ||
err := msoClient.CreateIntersiteL3outs(&l3outMap) | ||
if err != nil { | ||
return err | ||
} | ||
l3outId := L3outModelToL3outId(&l3outMap) | ||
d.SetId(l3outId) | ||
log.Printf("[DEBUG] Schema Site L3out: Creation Completed") | ||
return resourceMSOSchemaSiteL3outRead(d, m) | ||
} | ||
|
||
func resourceMSOSchemaSiteL3outRead(d *schema.ResourceData, m interface{}) error { | ||
log.Println("[DEBUG] Schema Site L3out: Beginning Read", d.Id()) | ||
msoClient := m.(*client.Client) | ||
id := d.Id() | ||
l3out, err := L3outIdToL3outModel(id) | ||
if err != nil { | ||
return err | ||
} | ||
l3outMapRemote, err := msoClient.ReadIntersiteL3outs(l3out) | ||
if err != nil { | ||
d.SetId("") | ||
return nil | ||
} | ||
setMSOSchemaSiteL3outAttributes(l3outMapRemote, d) | ||
d.SetId(L3outModelToL3outId(l3out)) | ||
log.Println("[DEBUG] Schema Site L3out: Reading Completed", d.Id()) | ||
return nil | ||
} | ||
|
||
func resourceMSOSchemaSiteL3outDelete(d *schema.ResourceData, m interface{}) error { | ||
log.Println("[DEBUG] Schema Site L3out: Beginning Destroy", d.Id()) | ||
msoClient := m.(*client.Client) | ||
id := d.Id() | ||
l3out, err := L3outIdToL3outModel(id) | ||
if err != nil { | ||
return err | ||
} | ||
err = msoClient.DeleteIntersiteL3outs(l3out) | ||
if err != nil { | ||
return err | ||
} | ||
log.Println("[DEBUG] Schema Site L3out: Beginning Destroy", d.Id()) | ||
d.SetId("") | ||
return err | ||
} | ||
|
||
func L3outModelToL3outId(m *models.IntersiteL3outs) string { | ||
return fmt.Sprintf("%s/site/%s/template/%s/vrf/%s/l3out/%s", m.SchemaID, m.SiteId, m.TemplateName, m.VRFName, m.L3outName) | ||
} | ||
|
||
func L3outIdToL3outModel(id string) (*models.IntersiteL3outs, error) { | ||
getAttributes := strings.Split(id, "/") | ||
if len(getAttributes) != 9 || getAttributes[1] != "site" || getAttributes[3] != "template" || getAttributes[5] != "vrf" || getAttributes[7] != "l3out" { | ||
return nil, fmt.Errorf("invalid mso_schema_site_l3out id format") | ||
} | ||
l3outMap := models.IntersiteL3outs{ | ||
SchemaID: getAttributes[0], | ||
SiteId: getAttributes[2], | ||
TemplateName: getAttributes[4], | ||
VRFName: getAttributes[6], | ||
L3outName: getAttributes[8], | ||
} | ||
return &l3outMap, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
--- | ||
layout: "mso" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need subcategory parameter here? |
||
page_title: "MSO: mso_schema_site_l3out" | ||
sidebar_current: "docs-mso-data-source-schema_site_l3out" | ||
description: |- | ||
Data source for MSO Schema Site L3out | ||
--- | ||
|
||
# mso_schema_site_l3out # | ||
|
||
Data source for MSO schema site L3out, to fetch the MSO schema site L3out details. | ||
|
||
## Example Usage ## | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add the GUI Information? |
||
```hcl | ||
data "mso_schema_site_l3out" "exmple" { | ||
vrf_name = mso_schema_site_vrf.example.vrf_name | ||
l3out_name = mso_schema_site_l3out.example.l3out_name | ||
template_name = mso_site.example.template_name | ||
site_id = mso_site.example.site_id | ||
schema_id = mso_site.example.schema_id | ||
} | ||
``` | ||
|
||
## Argument Reference ## | ||
* `schema_id` - (Required) The schema-id where L3out is added. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the L3Out? |
||
* `l3out_name` - (Required) Name of the added L3out. | ||
* `template_name` - (Required) Template name associated with the L3out. | ||
* `vrf_name` - (Required) VRF name associated with the L3out. | ||
* `site_id` - (Required) SiteID associated with the L3out. | ||
|
||
## Attribute Reference ## | ||
|
||
No attributes are exported. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got "Missing required argument" and "Reference to undeclared resource" error messages. While running the example.