Skip to content

Commit

Permalink
Merge pull request #343 from citrix/service_state_fix
Browse files Browse the repository at this point in the history
Update service resource to correctly parse disabled state
  • Loading branch information
George Nikolopoulos authored Mar 2, 2022
2 parents 0a84a50 + 2da79f3 commit 84f7512
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
84 changes: 84 additions & 0 deletions citrixadc/resource_citrixadc_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package citrixadc
import (
"fmt"
"log"
"time"

"github.com/citrix/adc-nitro-go/resource/config/basic"
"github.com/citrix/adc-nitro-go/resource/config/lb"
Expand Down Expand Up @@ -311,6 +312,27 @@ func resourceCitrixAdcService() *schema.Resource {
Optional: true,
Computed: true,
},
// Wait for disabled state parameters
"wait_until_disabled": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"disabled_timeout": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "2m",
},
"disabled_poll_delay": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "2s",
},
"disabled_poll_interval": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "5s",
},
},
}
}
Expand Down Expand Up @@ -537,6 +559,13 @@ func readServiceFunc(d *schema.ResourceData, meta interface{}) error {
d.Set("usip", data["usip"])
d.Set("weight", data["weight"])

// Set state according to svrstate
if data["svrstate"] == "OUT OF SERVICE" {
d.Set("state", "DISABLED")
} else {
d.Set("state", "ENABLED")
}

var boundVserver string
if _, ok := d.GetOk("lbvserver"); ok {
for _, vserver := range vserverBindings {
Expand Down Expand Up @@ -974,6 +1003,10 @@ func doServiceStateChange(d *schema.ResourceData, client *service.NitroClient) e
if err != nil {
return err
}
// Wait for state change
if d.Get("wait_until_disabled").(bool) {
serviceWaitDisableState(d, client)
}
} else {
return fmt.Errorf("\"%s\" is not a valid state. Use (\"ENABLED\", \"DISABLED\").", newstate)
}
Expand Down Expand Up @@ -1038,3 +1071,54 @@ func syncSslservice(d *schema.ResourceData, client *service.NitroClient) error {

return nil
}

func serviceWaitDisableState(d *schema.ResourceData, client *service.NitroClient) error {
log.Printf("[DEBUG] citrixadc-provider: In serviceWaitDisableState")

var err error
var timeout time.Duration
if timeout, err = time.ParseDuration(d.Get("disabled_timeout").(string)); err != nil {
return err
}

var poll_interval time.Duration
if poll_interval, err = time.ParseDuration(d.Get("disabled_poll_interval").(string)); err != nil {
return err
}

var poll_delay time.Duration
if poll_delay, err = time.ParseDuration(d.Get("disabled_poll_delay").(string)); err != nil {
return err
}
stateConf := &resource.StateChangeConf{
Pending: []string{"enabled"},
Target: []string{"disabled"},
Refresh: serviceStatePoll(d, client),
Timeout: timeout,
PollInterval: poll_interval,
Delay: poll_delay,
}

_, err = stateConf.WaitForState()
if err != nil {
return err
}

return nil
}

func serviceStatePoll(d *schema.ResourceData, client *service.NitroClient) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
log.Printf("[DEBUG] citrixadc-provider: In serviceStatePoll")
serviceName := d.Id()
data, err := client.FindResource(service.Service.Type(), serviceName)
if err != nil {
return nil, "", err
}
if data["svrstate"] == "OUT OF SERVICE" {
return "disabled", "disabled", nil
} else {
return "enabled", "enabled", nil
}
}
}
2 changes: 2 additions & 0 deletions citrixadc/resource_citrixadc_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ resource "citrixadc_service" "tf_acc_service" {
state = "ENABLED"
graceful = "YES"
delay = 60
wait_until_disabled = true
}
`

Expand All @@ -255,6 +256,7 @@ resource "citrixadc_service" "tf_acc_service" {
state = "DISABLED"
graceful = "YES"
delay = 60
wait_until_disabled = true
}
`

Expand Down
4 changes: 4 additions & 0 deletions docs/resources/service.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ resource "citrixadc_service" "tf_service" {
* `riseapbrstatsmsgcode` - (Optional)
* `accessdown` - (Optional)
* `appflowlog` - (Optional)
* `wait_until_disabled` - (Optional) Boolean flag to signify if the resource will wait for the service to be in a disabled state after the disable operation has been issued.
* `disabled_timeout` - (Optional) Time period to wait for the service to be in a disabled state after the disable operation.
* `disabled_poll_delay` - (Optional) Time period to wait before the first poll for the disabled state read.
* `disabled_poll_interval` - (Optional) Time period for disabled state read poll interval between tries.


## Attribute Reference
Expand Down
3 changes: 3 additions & 0 deletions examples/service/resources.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ resource "citrixadc_service" "tf_service" {
ipaddress = "10.77.33.22"
ip = "10.77.33.22"
port = "80"

state = "ENABLED"
wait_until_disabled = true
}

0 comments on commit 84f7512

Please sign in to comment.