Skip to content

Commit

Permalink
Merge pull request #208 from MerryPlant/systemLicence
Browse files Browse the repository at this point in the history
Fixes #205: Added support for system license
  • Loading branch information
jeremmfr authored May 17, 2021
2 parents cc8ad55 + 898907a commit 204f512
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
115 changes: 115 additions & 0 deletions junos/resource_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
jdecode "github.com/jeremmfr/junosdecode"
)

type systemOptions struct {
Expand All @@ -29,6 +30,7 @@ type systemOptions struct {
nameServer []string
inet6BackupRouter []map[string]interface{}
internetOptions []map[string]interface{}
license []map[string]interface{}
login []map[string]interface{}
services []map[string]interface{}
syslog []map[string]interface{}
Expand Down Expand Up @@ -234,6 +236,38 @@ func resourceSystem() *schema.Resource {
},
},
},
"license": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"autoupdate_password": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
RequiredWith: []string{"license.0.autoupdate_url"},
},
"autoupdate_url": {
Type: schema.TypeString,
Optional: true,
},
"renew_before_expiration": {
Type: schema.TypeInt,
Default: -1,
Optional: true,
ValidateFunc: validation.IntBetween(0, 60),
RequiredWith: []string{"license.0.renew_interval"},
},
"renew_interval": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(1, 336),
RequiredWith: []string{"license.0.renew_before_expiration"},
},
},
},
},
"login": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -793,6 +827,29 @@ func setSystem(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) e
if err := setSystemInternetOptions(d, m, jnprSess); err != nil {
return err
}
for _, v := range d.Get("license").([]interface{}) {
setPrefixLicense := setPrefix + " license"
if v != nil {
license := v.(map[string]interface{})
if license["autoupdate_url"].(string) != "" {
setPrefixLicenseUpdate := setPrefixLicense + " autoupdate url " + license["autoupdate_url"].(string)
if license["autoupdate_password"].(string) != "" {
configSet = append(configSet, setPrefixLicenseUpdate+" password "+
license["autoupdate_password"].(string))
} else {
configSet = append(configSet, setPrefixLicenseUpdate)
}
}
if license["renew_before_expiration"].(int) != -1 {
configSet = append(configSet, setPrefixLicense+" renew before-expiration "+
strconv.Itoa(license["renew_before_expiration"].(int)))
}
if license["renew_interval"].(int) > 0 {
configSet = append(configSet, setPrefixLicense+" renew interval "+
strconv.Itoa(license["renew_interval"].(int)))
}
}
}
if err := setSystemLogin(d, m, jnprSess); err != nil {
return err
}
Expand Down Expand Up @@ -1228,6 +1285,14 @@ func listLinesLogin() []string {
}
}

func listLinesLicense() []string {
return []string{
"license autoupdate",
"license renew",
"license",
}
}

func listLinesServices() []string {
ls := make([]string, 0)
ls = append(ls, listLinesServicesSSH()...)
Expand Down Expand Up @@ -1285,6 +1350,7 @@ func delSystem(m interface{}, jnprSess *NetconfObject) error {
listLinesToDelete = append(listLinesToDelete, "host-name")
listLinesToDelete = append(listLinesToDelete, "inet6-backup-router")
listLinesToDelete = append(listLinesToDelete, "internet-options")
listLinesToDelete = append(listLinesToDelete, listLinesLicense()...)
listLinesToDelete = append(listLinesToDelete, listLinesLogin()...)
listLinesToDelete = append(listLinesToDelete, "max-configuration-rollbacks")
listLinesToDelete = append(listLinesToDelete, "max-configurations-on-flash")
Expand Down Expand Up @@ -1362,6 +1428,10 @@ func readSystem(m interface{}, jnprSess *NetconfObject) (systemOptions, error) {
if err := readSystemInternetOptions(&confRead, itemTrim); err != nil {
return confRead, err
}
case strings.HasPrefix(itemTrim, "license "):
if err := readSystemLicense(&confRead, itemTrim); err != nil {
return confRead, err
}
case checkStringHasPrefixInList(itemTrim, listLinesLogin()):
if err := readSystemLogin(&confRead, itemTrim); err != nil {
return confRead, err
Expand Down Expand Up @@ -1739,6 +1809,48 @@ func readSystemInternetOptions(confRead *systemOptions, itemTrim string) error {
return nil
}

func readSystemLicense(confRead *systemOptions, itemTrim string) error {
if len(confRead.license) == 0 {
confRead.license = append(confRead.license, map[string]interface{}{
"autoupdate_password": "",
"autoupdate_url": "",
"renew_before_expiration": -1,
"renew_interval": 0,
})
}
switch {
case strings.HasPrefix(itemTrim, "license autoupdate url "):
itemTrimAutoupdateSplit := strings.Split(strings.TrimPrefix(itemTrim, "license autoupdate url "), " ")
confRead.license[0]["autoupdate_url"] = itemTrimAutoupdateSplit[0]

itemTrimPassword := strings.TrimPrefix(itemTrim, "license autoupdate url "+itemTrimAutoupdateSplit[0]+" ")
if strings.HasPrefix(itemTrimPassword, "password ") {
var err error
confRead.license[0]["autoupdate_password"], err = jdecode.Decode(strings.Trim(strings.TrimPrefix(
itemTrimPassword, "password "), "\""))
if err != nil {
return fmt.Errorf("failed to decode password : %w", err)
}
}
case strings.HasPrefix(itemTrim, "license renew before-expiration "):
var err error
confRead.license[0]["renew_before_expiration"], err =
strconv.Atoi(strings.TrimPrefix(itemTrim, "license renew before-expiration "))
if err != nil {
return fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
}
case strings.HasPrefix(itemTrim, "license renew interval "):
var err error
confRead.license[0]["renew_interval"], err =
strconv.Atoi(strings.TrimPrefix(itemTrim, "license renew interval "))
if err != nil {
return fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
}
}

return nil
}

func readSystemServicesSSH(confRead *systemOptions, itemTrim string) error {
if len(confRead.services[0]["ssh"].([]map[string]interface{})) == 0 {
confRead.services[0]["ssh"] = append(confRead.services[0]["ssh"].([]map[string]interface{}),
Expand Down Expand Up @@ -2007,6 +2119,9 @@ func fillSystem(d *schema.ResourceData, systemOptions systemOptions) {
if tfErr := d.Set("internet_options", systemOptions.internetOptions); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("license", systemOptions.license); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("login", systemOptions.login); tfErr != nil {
panic(tfErr)
}
Expand Down
16 changes: 16 additions & 0 deletions junos/resource_system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ func TestAccJunosSystem_basic(t *testing.T) {
"internet_options.0.tcp_drop_synfin_set", "true"),
resource.TestCheckResourceAttr("junos_system.testacc_system",
"internet_options.0.tcp_mss", "1400"),
resource.TestCheckResourceAttr("junos_system.testacc_system",
"license.#", "1"),
resource.TestCheckResourceAttr("junos_system.testacc_system",
"license.0.autoupdate_password", "some_password"),
resource.TestCheckResourceAttr("junos_system.testacc_system",
"license.0.autoupdate_url", "some_url"),
resource.TestCheckResourceAttr("junos_system.testacc_system",
"license.0.renew_interval", "24"),
resource.TestCheckResourceAttr("junos_system.testacc_system",
"license.0.renew_before_expiration", "30"),
resource.TestCheckResourceAttr("junos_system.testacc_system",
"login.#", "1"),
resource.TestCheckResourceAttr("junos_system.testacc_system",
Expand Down Expand Up @@ -271,6 +281,12 @@ resource junos_system "testacc_system" {
tcp_drop_synfin_set = true
tcp_mss = 1400
}
license {
autoupdate_password = "some_password"
autoupdate_url = "some_url"
renew_interval = 24
renew_before_expiration = 30
}
login {
announcement = "test announce"
deny_sources_address = ["127.0.0.1"]
Expand Down
5 changes: 5 additions & 0 deletions website/docs/r/system.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ The following arguments are supported:
* `address` - (Optional)(`String`) Address of router to use while booting.
* `destination` - (Optional)(`ListOfString`) Destination networks reachable through the router.
* `internet_options` - (Optional)([attribute-as-blocks mode](https://www.terraform.io/docs/configuration/attr-as-blocks.html)) Can be specified only once to declare 'internet-options' configuration. See the [`internet_options` arguments] (#internet_options-arguments) block.
* `license` - (Optional)([attribute-as-blocks mode](https://www.terraform.io/docs/configuration/attr-as-blocks.html)) Can be specified once to declare 'license' configuration.
* `autoupdate_password` - (Optional)(`String`) Password for autoupdate license keys from license servers. `autoupdate_url` needs to be set.
* `autoupdate_url` - (Optional)(`String`) Url for autoupdate license keys from license servers.
* `renew_before_expiration` - (Optional)(`Int`) License renewal lead time before expiration, in days (0..60). `renew_interval` needs to be set.
* `renew_interval` - (Optional)(`Int`) License checking interval, in hours (1..336). `renew_before_expiration` needs to be set.
* `login` - (Optional)([attribute-as-blocks mode](https://www.terraform.io/docs/configuration/attr-as-blocks.html)) Can be specified only once to declare 'login' configuration. See the [`login` arguments] (#login-arguments) block.
* `max_configuration_rollbacks` - (Optional)(`Int`) Maximum rollback configuration (0..49).
* `max_configurations_on_flash` - (Optional)(`Int`) Number of configuration files stored on flash (0..49).
Expand Down

0 comments on commit 204f512

Please sign in to comment.