Skip to content

Commit

Permalink
fixes some errors in PR #205
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremmfr committed May 17, 2021
1 parent 204f512 commit c1d60e4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
52 changes: 33 additions & 19 deletions junos/resource_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,20 @@ func resourceSystem() *schema.Resource {
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"autoupdate": {
Type: schema.TypeBool,
Optional: true,
},
"autoupdate_password": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
RequiredWith: []string{"license.0.autoupdate_url"},
},
"autoupdate_url": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
RequiredWith: []string{"license.0.autoupdate"},
},
"renew_before_expiration": {
Type: schema.TypeInt,
Expand Down Expand Up @@ -828,26 +833,32 @@ func setSystem(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) e
return err
}
for _, v := range d.Get("license").([]interface{}) {
setPrefixLicense := setPrefix + " license"
if v != nil {
license := v.(map[string]interface{})
setPrefixLicense := setPrefix + "license "
license := v.(map[string]interface{})
if license["autoupdate"].(bool) {
configSet = append(configSet, setPrefixLicense+"autoupdate")
if license["autoupdate_url"].(string) != "" {
setPrefixLicenseUpdate := setPrefixLicense + " autoupdate url " + 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))
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)))
}
} else if license["autoupdate_url"].(string) != "" {
return fmt.Errorf("license.0.autoupdate need to be true")
}
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 !strings.HasPrefix(configSet[len(configSet)-1], setPrefixLicense) {
return fmt.Errorf("license block is empty")
}
}
if err := setSystemLogin(d, m, jnprSess); err != nil {
Expand Down Expand Up @@ -1289,7 +1300,6 @@ func listLinesLicense() []string {
return []string{
"license autoupdate",
"license renew",
"license",
}
}

Expand Down Expand Up @@ -1428,7 +1438,7 @@ func readSystem(m interface{}, jnprSess *NetconfObject) (systemOptions, error) {
if err := readSystemInternetOptions(&confRead, itemTrim); err != nil {
return confRead, err
}
case strings.HasPrefix(itemTrim, "license "):
case checkStringHasPrefixInList(itemTrim, listLinesLicense()):
if err := readSystemLicense(&confRead, itemTrim); err != nil {
return confRead, err
}
Expand Down Expand Up @@ -1812,16 +1822,20 @@ func readSystemInternetOptions(confRead *systemOptions, itemTrim string) error {
func readSystemLicense(confRead *systemOptions, itemTrim string) error {
if len(confRead.license) == 0 {
confRead.license = append(confRead.license, map[string]interface{}{
"autoupdate": false,
"autoupdate_password": "",
"autoupdate_url": "",
"renew_before_expiration": -1,
"renew_interval": 0,
})
}
switch {
case itemTrim == "license autoupdate":
confRead.license[0]["autoupdate"] = true
case strings.HasPrefix(itemTrim, "license autoupdate url "):
confRead.license[0]["autoupdate"] = true
itemTrimAutoupdateSplit := strings.Split(strings.TrimPrefix(itemTrim, "license autoupdate url "), " ")
confRead.license[0]["autoupdate_url"] = itemTrimAutoupdateSplit[0]
confRead.license[0]["autoupdate_url"] = strings.Trim(itemTrimAutoupdateSplit[0], "\"")

itemTrimPassword := strings.TrimPrefix(itemTrim, "license autoupdate url "+itemTrimAutoupdateSplit[0]+" ")
if strings.HasPrefix(itemTrimPassword, "password ") {
Expand Down
1 change: 1 addition & 0 deletions junos/resource_system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ resource junos_system "testacc_system" {
tcp_mss = 1400
}
license {
autoupdate = true
autoupdate_password = "some_password"
autoupdate_url = "some_url"
renew_interval = 24
Expand Down
3 changes: 2 additions & 1 deletion website/docs/r/system.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ The following arguments are supported:
* `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` - (Optional)(`Bool`) Enable autoupdate license keys.
* `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.
* `autoupdate_url` - (Optional)(`String`) Url for autoupdate license keys from license servers. `autoupdate` needs to be set.
* `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.
Expand Down

0 comments on commit c1d60e4

Please sign in to comment.