Skip to content

Commit

Permalink
Merge pull request #97 from citrix/new_resources
Browse files Browse the repository at this point in the history
New resources
  • Loading branch information
George Nikolopoulos authored May 7, 2020
2 parents d82d9b2 + d3fbf48 commit d0311ea
Show file tree
Hide file tree
Showing 26 changed files with 1,006 additions and 17 deletions.
3 changes: 3 additions & 0 deletions citrixadc/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ func providerResources() map[string]*schema.Resource {
"citrixadc_auditmessageaction": resourceCitrixAdcAuditmessageaction(),
"citrixadc_auditsyslogaction": resourceCitrixAdcAuditsyslogaction(),
"citrixadc_auditsyslogpolicy": resourceCitrixAdcAuditsyslogpolicy(),
"citrixadc_rebooter": resourceCitrixAdcRebooter(),
"citrixadc_installer": resourceCitrixAdcInstaller(),
"citrixadc_pinger": resourceCitrixAdcPinger(),
}
}

Expand Down
11 changes: 10 additions & 1 deletion citrixadc/resource_citrixadc_cspolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ func resourceCitrixAdcCspolicy() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"forcenew_id_set": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Computed: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"priority": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -194,12 +201,14 @@ func readCspolicyFunc(d *schema.ResourceData, meta interface{}) error {
}
var boundCsvserver string
for _, binding := range bindings {
log.Printf("[TRACE] netscaler-provider: csvserver_cspolicy binding %v", binding)
csv, ok := binding["domain"]
if ok {
boundCsvserver = csv.(string)
break
}
}
log.Printf("[TRACE] netscaler-provider: boundCsvserver %v", boundCsvserver)
d.Set("csvserver", boundCsvserver)

return nil
Expand Down Expand Up @@ -312,7 +321,7 @@ func deleteCspolicyFunc(d *schema.ResourceData, meta interface{}) error {
//First we unbind from cs vserver if necessary
err := client.UnbindResource(netscaler.Csvserver.Type(), csvserver, netscaler.Cspolicy.Type(), cspolicyName, "policyname")
if err != nil {
return fmt.Errorf("[ERROR] netscaler-provider: Error unbinding cspolicy from csvserver %s", cspolicyName)
return fmt.Errorf("[ERROR] netscaler-provider: Error unbinding cspolicy \"%s\" from csvserver \"%v\": %v ", cspolicyName, csvserver, err.Error())
}
err = client.DeleteResource(netscaler.Cspolicy.Type(), cspolicyName)
if err != nil {
Expand Down
220 changes: 220 additions & 0 deletions citrixadc/resource_citrixadc_installer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
package citrixadc

import (
"github.com/chiradeep/go-nitro/config/utility"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"

"bytes"
"fmt"
"log"
"net/http"
"strings"
"time"
)

func resourceCitrixAdcInstaller() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
Create: createInstallerFunc,
Read: schema.Noop,
Delete: schema.Noop,
Schema: map[string]*schema.Schema{
"enhancedupgrade": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Computed: true,
ForceNew: true,
},
"l": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Computed: true,
ForceNew: true,
},
"resizeswapvar": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Computed: true,
ForceNew: true,
},
"url": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"y": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Computed: true,
ForceNew: true,
},
"wait_until_reachable": &schema.Schema{
Type: schema.TypeBool,
Required: true,
ForceNew: true,
},
"reachable_timeout": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "10m",
ForceNew: true,
},
"reachable_poll_delay": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "60s",
ForceNew: true,
},
"reachable_poll_interval": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "60s",
ForceNew: true,
},
"reachable_poll_timeout": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "20s",
ForceNew: true,
},
},
}
}

func createInstallerFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In createInstallFunc")
installerId := resource.PrefixedUniqueId("tf-installer-")

err := installerInstallBuild(d, meta)
if err != nil {
return err
}

if d.Get("wait_until_reachable").(bool) {
err := installerWaitReachable(d, meta)
if err != nil {
return err
}
}

d.SetId(installerId)

return nil
}

func installerInstallBuild(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In installerInstallBuild")

client := meta.(*NetScalerNitroClient).client

install := utility.Install{
Enhancedupgrade: d.Get("enhancedupgrade").(bool),
L: d.Get("l").(bool),
Resizeswapvar: d.Get("resizeswapvar").(bool),
Url: d.Get("url").(string),
Y: d.Get("y").(bool),
}

if err := client.ActOnResource("install", &install, ""); err != nil {
errorStr := err.Error()
if strings.HasSuffix(errorStr, "EOF") || strings.HasSuffix(errorStr, "connection reset by peer") {
// This is expected since the operation results in a TCP conection reset some times
// especially when y = true
log.Printf("[DEBUG] citrixadc-provider: Ignoring go-nitro error \"%s\"", errorStr)
return nil
} else {
return err
}
}
return nil
}

func installerWaitReachable(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In installerWaitReachable")

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

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

var poll_delay time.Duration
if poll_delay, err = time.ParseDuration(d.Get("reachable_poll_delay").(string)); err != nil {
return err
}
stateConf := &resource.StateChangeConf{
Pending: []string{"unreachable"},
Target: []string{"reachable"},
Refresh: installerInstancePoll(d, meta),
Timeout: timeout,
PollInterval: poll_interval,
Delay: poll_delay,
}

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

return nil
}

func installerPollLicense(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In installerPollLicense")

username := meta.(*NetScalerNitroClient).Username
password := meta.(*NetScalerNitroClient).Password
endpoint := meta.(*NetScalerNitroClient).Endpoint
url := fmt.Sprintf("%s/nitro/v1/config/nslicense", endpoint)

var timeout time.Duration
var err error
if timeout, err = time.ParseDuration(d.Get("reachable_poll_timeout").(string)); err != nil {
return err
}
c := http.Client{
Timeout: timeout,
}
buff := &bytes.Buffer{}
req, _ := http.NewRequest("GET", url, buff)
req.Header.Set("X-NITRO-USER", username)
req.Header.Set("X-NITRO-PASS", password)
resp, err := c.Do(req)
if err != nil {
if !strings.Contains(err.Error(), "Client.Timeout exceeded") {
// Unexpected error
return err
} else {
// Expected timeout error
return fmt.Errorf("Timeout")
}
} else {
log.Printf("Status code is %v\n", resp.Status)
}
// No error
return nil
}

func installerInstancePoll(d *schema.ResourceData, meta interface{}) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
log.Printf("[DEBUG] citrixadc-provider: In installerInstancePoll")
err := installerPollLicense(d, meta)
if err != nil {
if err.Error() == "Timeout" {
return nil, "unreachable", nil
} else {
return nil, "unreachable", err
}
}
log.Printf("[DEBUG] citrixadc-provider: Returning \"reachable\"")
return "reachable", "reachable", nil
}
}
77 changes: 77 additions & 0 deletions citrixadc/resource_citrixadc_installer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
Copyright 2016 Citrix Systems, Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package citrixadc

import (
"fmt"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"testing"
)

func TestAccInstaller_basic(t *testing.T) {
if isCpxRun {
t.Skip("Install not available in CPX")
}
if isCluster {
t.Skip("Install not available in Cluster")
}
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccInstaller_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckInstallExist("citrixadc_installer.tf_installer", nil),
),
},
},
})
}

func testAccCheckInstallExist(n string, id *string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No lb vserver name is set")
}

if id != nil {
if *id != "" && *id != rs.Primary.ID {
return fmt.Errorf("Resource ID has changed!")
}

*id = rs.Primary.ID
}

return nil
}
}

const testAccInstaller_basic = `
resource "citrixadc_installer" "tf_installer" {
#url = "file:///var/tmp/build_mana_52_24_nc_64.tgz"
url = "file:///var/tmp/build_mana_47_24_nc_64.tgz"
y = true
l = false
wait_until_reachable = true
}
`
Loading

0 comments on commit d0311ea

Please sign in to comment.