From 2da79f399d2f8fd7a7cce69d9bf5788c86337468 Mon Sep 17 00:00:00 2001 From: George Nikolopoulos Date: Wed, 2 Mar 2022 14:18:20 +0200 Subject: [PATCH] Update service resource to correctly parse disabled state --- citrixadc/resource_citrixadc_service.go | 84 ++++++++++++++++++++ citrixadc/resource_citrixadc_service_test.go | 2 + docs/resources/service.md | 4 + examples/service/resources.tf | 3 + 4 files changed, 93 insertions(+) diff --git a/citrixadc/resource_citrixadc_service.go b/citrixadc/resource_citrixadc_service.go index 354e64e52..9a7f47d79 100644 --- a/citrixadc/resource_citrixadc_service.go +++ b/citrixadc/resource_citrixadc_service.go @@ -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" @@ -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", + }, }, } } @@ -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 { @@ -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) } @@ -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 + } + } +} diff --git a/citrixadc/resource_citrixadc_service_test.go b/citrixadc/resource_citrixadc_service_test.go index 946badfff..aa3a770f0 100644 --- a/citrixadc/resource_citrixadc_service_test.go +++ b/citrixadc/resource_citrixadc_service_test.go @@ -229,6 +229,7 @@ resource "citrixadc_service" "tf_acc_service" { state = "ENABLED" graceful = "YES" delay = 60 + wait_until_disabled = true } ` @@ -255,6 +256,7 @@ resource "citrixadc_service" "tf_acc_service" { state = "DISABLED" graceful = "YES" delay = 60 + wait_until_disabled = true } ` diff --git a/docs/resources/service.md b/docs/resources/service.md index 029a60fec..8f8ea6e69 100644 --- a/docs/resources/service.md +++ b/docs/resources/service.md @@ -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 diff --git a/examples/service/resources.tf b/examples/service/resources.tf index a194e8e73..b6f5ffc6a 100644 --- a/examples/service/resources.tf +++ b/examples/service/resources.tf @@ -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 }