From 4db2e8afcb79e098d9237fceab6f148d1ec9e5b9 Mon Sep 17 00:00:00 2001 From: Thiago Uehara Date: Thu, 26 Aug 2021 16:59:30 -0300 Subject: [PATCH 1/3] Fixed TestAccRecord_meta --- ns1/resource_record_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ns1/resource_record_test.go b/ns1/resource_record_test.go index abbf55b7..b1671ef4 100644 --- a/ns1/resource_record_test.go +++ b/ns1/resource_record_test.go @@ -108,8 +108,8 @@ func TestAccRecord_meta(t *testing.T) { zoneName := fmt.Sprintf("terraform-test-%s.io", rString) domainName := fmt.Sprintf("test.%s", zoneName) sub := make(map[string]interface{}, 2) - sub["DZ"] = []string{"01", "02", "03"} sub["BR"] = []string{"SC", "SP"} + sub["DZ"] = []string{"01", "02", "03"} resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, From 86c5d990391964f423f55fdf43a4f5435a42309c Mon Sep 17 00:00:00 2001 From: Thiago Uehara Date: Fri, 27 Aug 2021 17:48:59 -0300 Subject: [PATCH 2/3] Support for Pulsar Jobs added --- ns1/examples/pulsar_job.tf | 42 ++ ns1/provider.go | 1 + ns1/resource_pulsar_job.go | 445 ++++++++++++++++ ns1/resource_pulsar_job_test.go | 649 +++++++++++++++++++++++ website/docs/r/pulsar_job.html .markdown | 65 +++ 5 files changed, 1202 insertions(+) create mode 100644 ns1/examples/pulsar_job.tf create mode 100644 ns1/resource_pulsar_job.go create mode 100644 ns1/resource_pulsar_job_test.go create mode 100644 website/docs/r/pulsar_job.html .markdown diff --git a/ns1/examples/pulsar_job.tf b/ns1/examples/pulsar_job.tf new file mode 100644 index 00000000..63b5c95b --- /dev/null +++ b/ns1/examples/pulsar_job.tf @@ -0,0 +1,42 @@ +resource "ns1_pulsarjob" "example" { + # Required + name = "" + appid = "" + typeid = "custom" + + /* Obs: + If typeid is "latency", host and url_path becomes required. + + If blend_metric_weights is setted, host, url_path, timestamp + and weights becomes required. + */ + + # Optional + active = false + shared = false + config = { + host = "my_host.com" + url_path = "/my_url_path" + https = false + http = false + request_timeout_millis = 123 + job_timeout_millis = 321 + use_xhr = false + static_values = false + } + blend_metric_weights = { + timestamp = 53 + } + weights { + name = "WeightName1" + weight = 3 + default_value = 5.2 + maximize = false + } + weights { + name = "myWeightName2" + weight = 0 + default_value = 5.22 + maximize = false + } +} \ No newline at end of file diff --git a/ns1/provider.go b/ns1/provider.go index 2842b6c7..a2c24745 100644 --- a/ns1/provider.go +++ b/ns1/provider.go @@ -59,6 +59,7 @@ func Provider() terraform.ResourceProvider { "ns1_user": userResource(), "ns1_apikey": apikeyResource(), "ns1_team": teamResource(), + "ns1_pulsarjob": pulsarJobResource(), }, ConfigureFunc: ns1Configure, } diff --git a/ns1/resource_pulsar_job.go b/ns1/resource_pulsar_job.go new file mode 100644 index 00000000..56233617 --- /dev/null +++ b/ns1/resource_pulsar_job.go @@ -0,0 +1,445 @@ +package ns1 + +import ( + "fmt" + "log" + "strconv" + "strings" + + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + + ns1 "gopkg.in/ns1/ns1-go.v2/rest" + "gopkg.in/ns1/ns1-go.v2/rest/model/pulsar" +) + +func pulsarJobResource() *schema.Resource { + s := map[string]*schema.Schema{ + "customer": { + Type: schema.TypeInt, + Computed: true, + }, + "name": { + Type: schema.TypeString, + Required: true, + }, + "typeid": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validateTypeId, + }, + "community": { + Type: schema.TypeBool, + Computed: true, + }, + "jobid": { + Type: schema.TypeString, + Computed: true, + ForceNew: true, + }, + "appid": { + Type: schema.TypeString, + Optional: true, + }, + "active": { + Type: schema.TypeBool, + Optional: true, + Default: true, + }, + "shared": { + Type: schema.TypeBool, + Optional: true, + Default: true, + }, + "config": { + Type: schema.TypeMap, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "host": { + Type: schema.TypeString, + Optional: true, + }, + "url_path": { + Type: schema.TypeString, + Optional: true, + }, + "http": { + Type: schema.TypeBool, + Optional: true, + }, + "https": { + Type: schema.TypeBool, + Optional: true, + }, + "request_timeout_millis": { + Type: schema.TypeInt, + Optional: true, + }, + "job_timeout_millis": { + Type: schema.TypeInt, + Optional: true, + }, + "use_xhr": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + "static_values": { + Type: schema.TypeBool, + Optional: true, + }, + }, + }, + }, + "blend_metric_weights": { + Type: schema.TypeMap, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "timestamp": { + Type: schema.TypeInt, + Required: true, + }, + }, + }, + }, + "weights": { + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + }, + "weight": { + Type: schema.TypeInt, + Required: true, + }, + "default_value": { + Type: schema.TypeFloat, + Required: true, + }, + "maximize": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + }, + }, + }, + } + + return &schema.Resource{ + Schema: s, + Create: PulsarJobCreate, + Read: pulsarJobRead, + Update: PulsarJobUpdate, + Delete: pulsarJobDelete, + Importer: &schema.ResourceImporter{State: pulsarJobImportStateFunc}, + SchemaVersion: 1, + } +} + +func pulsarJobToResourceData(d *schema.ResourceData, j *pulsar.PulsarJob) error { + d.SetId(j.JobID) + d.Set("customer", j.Customer) + d.Set("name", j.Name) + d.Set("typeid", j.TypeID) + d.Set("community", j.Community) + d.Set("appid", j.AppID) + d.Set("active", j.Active) + d.Set("shared", j.Shared) + + if j.Config != nil { + if err := jobConfigToResourceData(d, j); err != nil { + return err + } + + } + + return nil +} + +func jobConfigToResourceData(d *schema.ResourceData, j *pulsar.PulsarJob) error { + config := make(map[string]interface{}) + if j.Config.Host != nil { + config["host"] = *j.Config.Host + } + if j.Config.URL_Path != nil { + config["url_path"] = *j.Config.URL_Path + } + if j.Config.Http != nil { + config["http"] = strconv.FormatBool(*j.Config.Http) + } + if j.Config.Https != nil { + config["https"] = strconv.FormatBool(*j.Config.Https) + } + if j.Config.RequestTimeoutMillis != nil { + config["request_timeout_millis"] = strconv.Itoa(*j.Config.RequestTimeoutMillis) + } + if j.Config.RequestTimeoutMillis != nil { + config["job_timeout_millis"] = strconv.Itoa(*j.Config.JobTimeoutMillis) + } + if j.Config.UseXHR != nil { + config["use_xhr"] = strconv.FormatBool(*j.Config.UseXHR) + } + if j.Config.StaticValues != nil { + config["static_values"] = strconv.FormatBool(*j.Config.StaticValues) + } + + if j.Config.BlendMetricWeights != nil { + if err := d.Set("blend_metric_weights", blendMetricWeightsToMap(j.Config.BlendMetricWeights)); err != nil { + return fmt.Errorf("[DEBUG] Error setting Blend Metric Weights for: %s, error: %#v", j.Name, err) + } + + if w := j.Config.BlendMetricWeights.Weights; w != nil { + weights := make([]map[string]interface{}, 0) + for _, weight := range w { + weights = append(weights, weightsToResourceData(weight)) + } + + if err := d.Set("weights", weights); err != nil { + return fmt.Errorf("[DEBUG] Error setting weights for: %s, error: %#v", j.Name, err) + } + } + } + + d.Set("config", config) + return nil +} + +func blendMetricWeightsToMap(b *pulsar.BlendMetricWeights) map[string]interface{} { + blendMetric := make(map[string]interface{}) + blendMetric["timestamp"] = strconv.Itoa(b.Timestamp) + + return blendMetric +} + +func weightsToResourceData(w *pulsar.Weights) map[string]interface{} { + weight := make(map[string]interface{}) + weight["name"] = w.Name + weight["weight"] = w.Weight + weight["default_value"] = w.DefaultValue + weight["maximize"] = w.Maximize + + return weight +} + +func resourceDataToPulsarJob(j *pulsar.PulsarJob, d *schema.ResourceData) error { + j.Name = d.Get("name").(string) + j.TypeID = d.Get("typeid").(string) + j.JobID = d.Id() + j.AppID = d.Get("appid").(string) + j.Active = d.Get("active").(bool) + j.Shared = d.Get("shared").(bool) + + if v, ok := d.GetOk("config"); ok { + if config, err := resourceDataToJobConfig(v); err != nil { + return err + } else { + j.Config = config + } + } + + if v, ok := d.GetOk("blend_metric_weights"); ok { + if j.Config == nil { + j.Config = &pulsar.JobConfig{} + } + + if m, err := resourceDataToBlendMetric(v); err != nil { + return err + } else { + j.Config.BlendMetricWeights = m + } + + if v, ok := d.GetOk("weights"); ok { + j.Config.BlendMetricWeights.Weights = resourceDataToWeights(v) + } + } + + return nil +} + +func resourceDataToJobConfig(v interface{}) (*pulsar.JobConfig, error) { + rawconfig := v.(map[string]interface{}) + j := &pulsar.JobConfig{} + + if v, ok := rawconfig["host"]; ok { + host := v.(string) + j.Host = &host + } + if v, ok := rawconfig["url_path"]; ok { + url_path := v.(string) + j.URL_Path = &url_path + } + if v, ok := rawconfig["http"]; ok { + if b, err := strconv.ParseBool(v.(string)); err != nil { + return nil, err + } else { + j.Http = &b + } + } + if v, ok := rawconfig["https"]; ok { + if b, err := strconv.ParseBool(v.(string)); err != nil { + return nil, err + } else { + j.Https = &b + } + } + if v, ok := rawconfig["request_timeout_millis"]; ok { + if i, err := strconv.Atoi(v.(string)); err != nil { + return nil, err + } else { + j.RequestTimeoutMillis = &i + } + } + if v, ok := rawconfig["job_timeout_millis"]; ok { + if i, err := strconv.Atoi(v.(string)); err != nil { + return nil, err + } else { + j.JobTimeoutMillis = &i + } + } + if v, ok := rawconfig["use_xhr"]; ok { + if b, err := strconv.ParseBool(v.(string)); err != nil { + return nil, err + } else { + j.UseXHR = &b + } + } + if v, ok := rawconfig["static_values"]; ok { + if b, err := strconv.ParseBool(v.(string)); err != nil { + return nil, err + } else { + j.StaticValues = &b + } + } + return j, nil +} + +func resourceDataToBlendMetric(v interface{}) (*pulsar.BlendMetricWeights, error) { + rawMetric := v.(map[string]interface{}) + m := &pulsar.BlendMetricWeights{} + + if v, ok := rawMetric["timestamp"]; ok { + if i, err := strconv.Atoi(v.(string)); err != nil { + return nil, err + } else { + m.Timestamp = i + } + } + + m.Weights = make([]*pulsar.Weights, 0) + + return m, nil +} + +func resourceDataToWeights(v interface{}) []*pulsar.Weights { + weightsSet := v.(*schema.Set) + weights := make([]*pulsar.Weights, weightsSet.Len()) + + for i, weightRaw := range weightsSet.List() { + weight := weightRaw.(map[string]interface{}) + weights[i] = &pulsar.Weights{ + Name: weight["name"].(string), + Weight: weight["weight"].(int), + DefaultValue: weight["default_value"].(float64), + Maximize: weight["maximize"].(bool), + } + } + + return weights +} + +// PulsarJobCreate creates the given Pulsar Job in ns1 +func PulsarJobCreate(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ns1.Client) + j := pulsar.PulsarJob{} + if err := resourceDataToPulsarJob(&j, d); err != nil { + return err + } + if resp, err := client.PulsarJobs.Create(&j); err != nil { + return ConvertToNs1Error(resp, err) + } + + return pulsarJobToResourceData(d, &j) +} + +// pulsarJobRead reads the given zone data from ns1 +func pulsarJobRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ns1.Client) + j, resp, err := client.PulsarJobs.Get(d.Get("appid").(string), d.Id()) + if err != nil { + if err == ns1.ErrAppMissing { + log.Printf("[DEBUG] NS1 Pulsar Application (%s) not found", d.Get("appid")) + d.SetId("") + return nil + } + + if err == ns1.ErrJobMissing { + log.Printf("[DEBUG] NS1 Pulsar Job (%s) not found", d.Get("jobid")) + d.SetId("") + return nil + } + + return ConvertToNs1Error(resp, err) + } + if err := resourceDataToPulsarJob(j, d); err != nil { + return err + } + return nil +} + +// PulsarJobUpdate updates the Pulsar Job with given parameters in ns1 +func PulsarJobUpdate(job_schema *schema.ResourceData, meta interface{}) error { + client := meta.(*ns1.Client) + j := pulsar.PulsarJob{ + JobID: job_schema.Id(), + AppID: job_schema.Get("appid").(string), + } + if err := resourceDataToPulsarJob(&j, job_schema); err != nil { + return err + } + + if resp, err := client.PulsarJobs.Update(&j); err != nil { + return ConvertToNs1Error(resp, err) + } + + return pulsarJobToResourceData(job_schema, &j) +} + +// pulsarJobDelete deletes the given Pulsar Job from ns1 +func pulsarJobDelete(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ns1.Client) + j := pulsar.PulsarJob{} + resourceDataToPulsarJob(&j, d) + resp, err := client.PulsarJobs.Delete(&j) + d.SetId("") + return ConvertToNs1Error(resp, err) +} + +func validateTypeId(val interface{}, key string) (warns []string, errs []error) { + v := val.(string) + if v != "latency" && v != "custom" { + errs = append(errs, + fmt.Errorf( + "typeid %s invalid, please select between 'latency' or 'custom'", v, + ), + ) + } + return warns, errs +} + +func pulsarJobImportStateFunc(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + parts := strings.Split(d.Id(), "_") + if len(parts) != 2 { + return nil, fmt.Errorf("invalid job specifier. Expected 2 ids (\"appid_jobid\", got %d)", len(parts)) + } + + d.Set("appid", parts[0]) + d.Set("jobid", parts[1]) + d.SetId(parts[1]) + + return []*schema.ResourceData{d}, nil +} diff --git a/ns1/resource_pulsar_job_test.go b/ns1/resource_pulsar_job_test.go new file mode 100644 index 00000000..f383edcb --- /dev/null +++ b/ns1/resource_pulsar_job_test.go @@ -0,0 +1,649 @@ +package ns1 + +import ( + "fmt" + "log" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/terraform" + + ns1 "gopkg.in/ns1/ns1-go.v2/rest" + "gopkg.in/ns1/ns1-go.v2/rest/model/pulsar" +) + +// Creating basic Pulsar jobs +func TestAccPulsarJob_basic(t *testing.T) { + var ( + job = pulsar.PulsarJob{} + jobName = fmt.Sprintf("terraform-test-%s.io", acctest.RandStringFromCharSet(15, acctest.CharSetAlphaNum)) + appid = "yv5kfn" + ) + // Basic test for JavaScript jobs + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckPulsarJobDestroy, + Steps: []resource.TestStep{ + { + Config: testAccJSPulsarJobBasic(jobName, appid), + Check: resource.ComposeTestCheckFunc( + testAccCheckPulsarJobExists("ns1_pulsarjob.it", &job), + testAccCheckPulsarJobName(&job, jobName), + testAccCheckPulsarJobTypeID(&job, "latency"), + testAccCheckPulsarJobAppID(&job, appid), + testAccCheckPulsarJobActive(&job, true), + testAccCheckPulsarJobShared(&job, true), + testAccCheckPulsarJobSCommunity(&job, false), + testAccCheckPulsarJobSHost(&job, "testAccHost"), + testAccCheckPulsarJobSUrlPath(&job, "/testAccURLPath"), + ), + }, + }, + }) + + // Basic test for Bulk Beacon jobs + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckPulsarJobDestroy, + Steps: []resource.TestStep{ + { + Config: testAccBBPulsarJobBasic(jobName, appid), + Check: resource.ComposeTestCheckFunc( + testAccCheckPulsarJobExists("ns1_pulsarjob.it", &job), + testAccCheckPulsarJobName(&job, jobName), + testAccCheckPulsarJobTypeID(&job, "custom"), + testAccCheckPulsarJobAppID(&job, "yv5kfn"), + testAccCheckPulsarJobActive(&job, true), + testAccCheckPulsarJobShared(&job, true), + testAccCheckPulsarJobSCommunity(&job, false), + ), + }, + }, + }) +} + +// Updating pulsar jobs without changing its type (JavaScript or Bulk Beacon) +func TestAccPulsarJob_updated_same_type(t *testing.T) { + var ( + job = pulsar.PulsarJob{} + jobName = fmt.Sprintf("terraform-test-%s.io", acctest.RandStringFromCharSet(15, acctest.CharSetAlphaNum)) + appid = "yv5kfn" + + updatedJobName = fmt.Sprintf("terraform-test-%s.io", acctest.RandStringFromCharSet(15, acctest.CharSetAlphaNum)) + ) + // Update test for JavaScript jobs + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckPulsarJobDestroy, + Steps: []resource.TestStep{ + { + Config: testAccJSPulsarJobBasic(jobName, appid), + Check: resource.ComposeTestCheckFunc( + testAccCheckPulsarJobExists("ns1_pulsarjob.it", &job), + testAccCheckPulsarJobName(&job, jobName), + testAccCheckPulsarJobTypeID(&job, "latency"), + testAccCheckPulsarJobAppID(&job, appid), + testAccCheckPulsarJobActive(&job, true), + testAccCheckPulsarJobShared(&job, true), + testAccCheckPulsarJobSCommunity(&job, false), + testAccCheckPulsarJobSHost(&job, "testAccHost"), + testAccCheckPulsarJobSUrlPath(&job, "/testAccURLPath"), + ), + }, + { + Config: testAccJSPulsarJobUpdated(updatedJobName, appid), + Check: resource.ComposeTestCheckFunc( + testAccCheckPulsarJobExists("ns1_pulsarjob.it", &job), + testAccCheckPulsarJobName(&job, updatedJobName), + testAccCheckPulsarJobTypeID(&job, "latency"), + testAccCheckPulsarJobAppID(&job, appid), + testAccCheckPulsarJobActive(&job, false), + testAccCheckPulsarJobShared(&job, false), + testAccCheckPulsarJobSCommunity(&job, false), + testAccCheckPulsarJobSHost(&job, "testAccUpdatedHost"), + testAccCheckPulsarJobSUrlPath(&job, "/testAccUpdatedURLPath"), + ), + }, + }, + }) + + // update test for Bulk Beacon jobs + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckPulsarJobDestroy, + Steps: []resource.TestStep{ + { + Config: testAccBBPulsarJobBasic(jobName, appid), + Check: resource.ComposeTestCheckFunc( + testAccCheckPulsarJobExists("ns1_pulsarjob.it", &job), + testAccCheckPulsarJobName(&job, jobName), + testAccCheckPulsarJobTypeID(&job, "custom"), + testAccCheckPulsarJobAppID(&job, "yv5kfn"), + testAccCheckPulsarJobActive(&job, true), + testAccCheckPulsarJobShared(&job, true), + testAccCheckPulsarJobSCommunity(&job, false), + ), + }, + { + Config: testAccBBPulsarJobUpdated(updatedJobName, appid), + Check: resource.ComposeTestCheckFunc( + testAccCheckPulsarJobExists("ns1_pulsarjob.it", &job), + testAccCheckPulsarJobName(&job, updatedJobName), + testAccCheckPulsarJobTypeID(&job, "custom"), + testAccCheckPulsarJobAppID(&job, "yv5kfn"), + testAccCheckPulsarJobActive(&job, false), + testAccCheckPulsarJobShared(&job, false), + testAccCheckPulsarJobSCommunity(&job, false), + ), + }, + }, + }) +} + +// Updating pulsar jobs changing its type (JavaScript <-> Bulk Beacon) +func TestAccPulsarJob_updated_different_type(t *testing.T) { + var ( + job = pulsar.PulsarJob{} + jobName = fmt.Sprintf("terraform-test-%s.io", acctest.RandStringFromCharSet(15, acctest.CharSetAlphaNum)) + appid = "yv5kfn" + + updatedJobName = fmt.Sprintf("terraform-test-%s.io", acctest.RandStringFromCharSet(15, acctest.CharSetAlphaNum)) + ) + + // Updating JavaScript job to Bulk Beacon Job + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckPulsarJobDestroy, + Steps: []resource.TestStep{ + { + Config: testAccJSPulsarJobBasic(jobName, appid), + Check: resource.ComposeTestCheckFunc( + testAccCheckPulsarJobExists("ns1_pulsarjob.it", &job), + testAccCheckPulsarJobName(&job, jobName), + testAccCheckPulsarJobTypeID(&job, "latency"), + testAccCheckPulsarJobAppID(&job, appid), + testAccCheckPulsarJobActive(&job, true), + testAccCheckPulsarJobShared(&job, true), + testAccCheckPulsarJobSCommunity(&job, false), + testAccCheckPulsarJobSHost(&job, "testAccHost"), + testAccCheckPulsarJobSUrlPath(&job, "/testAccURLPath"), + ), + }, + { + Config: testAccBBPulsarJobConverted(updatedJobName, appid), + Check: resource.ComposeTestCheckFunc( + testAccCheckPulsarJobExists("ns1_pulsarjob.it", &job), + testAccCheckPulsarJobName(&job, updatedJobName), + testAccCheckPulsarJobTypeID(&job, "custom"), + testAccCheckPulsarJobAppID(&job, "yv5kfn"), + testAccCheckPulsarJobActive(&job, true), + testAccCheckPulsarJobShared(&job, true), + testAccCheckPulsarJobSCommunity(&job, false), + ), + }, + }, + }) + + // Updating Bulk Beacon Job to JavaScript job + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckPulsarJobDestroy, + Steps: []resource.TestStep{ + { + Config: testAccBBPulsarJobBasic(jobName, appid), + Check: resource.ComposeTestCheckFunc( + testAccCheckPulsarJobExists("ns1_pulsarjob.it", &job), + testAccCheckPulsarJobName(&job, jobName), + testAccCheckPulsarJobTypeID(&job, "custom"), + testAccCheckPulsarJobAppID(&job, "yv5kfn"), + testAccCheckPulsarJobActive(&job, true), + testAccCheckPulsarJobShared(&job, true), + testAccCheckPulsarJobSCommunity(&job, false), + ), + }, + { + Config: testAccJSPulsarJobUpdated(updatedJobName, appid), + Check: resource.ComposeTestCheckFunc( + testAccCheckPulsarJobExists("ns1_pulsarjob.it", &job), + testAccCheckPulsarJobName(&job, updatedJobName), + testAccCheckPulsarJobTypeID(&job, "latency"), + testAccCheckPulsarJobAppID(&job, appid), + testAccCheckPulsarJobActive(&job, false), + testAccCheckPulsarJobShared(&job, false), + testAccCheckPulsarJobSCommunity(&job, false), + testAccCheckPulsarJobSHost(&job, "testAccUpdatedHost"), + testAccCheckPulsarJobSUrlPath(&job, "/testAccUpdatedURLPath"), + ), + }, + }, + }) +} + +// Creating Pulsar jobs with Blend Metric Weights +func TestAccPulsarJob_BlendMetricWeights(t *testing.T) { + var ( + job = pulsar.PulsarJob{} + jobName = fmt.Sprintf("terraform-test-%s.io", acctest.RandStringFromCharSet(15, acctest.CharSetAlphaNum)) + appid = "yv5kfn" + + weights = []pulsar.Weights{ + { + Name: "testAccWeight1", + Weight: 123, + DefaultValue: 12.3, + Maximize: false, + }, + { + Name: "testAccWeight2", + Weight: 321, + DefaultValue: 32.1, + Maximize: true, + }, + } + ) + + // Blend Metric Weights test for JavaScript jobs + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckPulsarJobDestroy, + Steps: []resource.TestStep{ + { + Config: testAccJSPulsarJobBlendMetricWeights(jobName, appid), + Check: resource.ComposeTestCheckFunc( + testAccCheckPulsarJobExists("ns1_pulsarjob.it", &job), + testAccCheckPulsarJobName(&job, jobName), + testAccCheckPulsarJobTypeID(&job, "latency"), + testAccCheckPulsarJobAppID(&job, appid), + testAccCheckPulsarJobActive(&job, true), + testAccCheckPulsarJobShared(&job, true), + testAccCheckPulsarJobSCommunity(&job, false), + testAccCheckPulsarJobSHost(&job, "testAccCompleteHost"), + testAccCheckPulsarJobSUrlPath(&job, "/testAccCompleteURLPath"), + testAccCHeckPulsarJobBlendMetricWeights_timestamp(&job, 123), + testAccCHeckPulsarJobBlendMetricWeights_weights(&job, weights), + ), + }, + }, + }) + + // Blend Metric Weights test for Bulk Beacon jobs + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckPulsarJobDestroy, + Steps: []resource.TestStep{ + { + Config: testAccBBPulsarJobBlendMetricWeights(jobName, appid), + Check: resource.ComposeTestCheckFunc( + testAccCheckPulsarJobExists("ns1_pulsarjob.it", &job), + testAccCheckPulsarJobName(&job, jobName), + testAccCheckPulsarJobTypeID(&job, "custom"), + testAccCheckPulsarJobSHost(&job, "testAccHost"), + testAccCheckPulsarJobSUrlPath(&job, "/testAccUrlPath"), + testAccCheckPulsarJobAppID(&job, appid), + testAccCheckPulsarJobActive(&job, true), + testAccCheckPulsarJobShared(&job, true), + testAccCheckPulsarJobSCommunity(&job, false), + testAccCHeckPulsarJobBlendMetricWeights_timestamp(&job, 123), + testAccCHeckPulsarJobBlendMetricWeights_weights(&job, weights), + ), + }, + }, + }) +} + +// Manually deleting Pulsar Jobs +func TestAccPulsarJob_ManualDelete(t *testing.T) { + var ( + job = pulsar.PulsarJob{} + jobName = fmt.Sprintf("terraform-test-%s.io", acctest.RandStringFromCharSet(15, acctest.CharSetAlphaNum)) + appid = "yv5kfn" + ) + // Manual deletion test for JavaScript jobs + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckPulsarJobDestroy, + Steps: []resource.TestStep{ + { + Config: testAccJSPulsarJobBasic(jobName, appid), + Check: resource.ComposeTestCheckFunc( + testAccCheckPulsarJobExists("ns1_pulsarjob.it", &job), + ), + }, + // Simulate a manual deletion of the pulsar job and verify that the plan has a diff. + { + PreConfig: testAccManualDeletePulsarJob(&job), + Config: testAccJSPulsarJobBasic(jobName, appid), + PlanOnly: true, + ExpectNonEmptyPlan: true, + }, + // Then re-create and make sure it is there again. + { + Config: testAccJSPulsarJobBasic(jobName, appid), + Check: testAccCheckPulsarJobExists("ns1_pulsarjob.it", &job), + }, + }, + }) + + // Manual deletion test for Bulk Beacon jobs + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckPulsarJobDestroy, + Steps: []resource.TestStep{ + { + Config: testAccBBPulsarJobBasic(jobName, appid), + Check: resource.ComposeTestCheckFunc( + testAccCheckPulsarJobExists("ns1_pulsarjob.it", &job), + ), + }, + // Simulate a manual deletion of the pulsar job and verify that the plan has a diff. + { + PreConfig: testAccManualDeletePulsarJob(&job), + Config: testAccBBPulsarJobBasic(jobName, appid), + PlanOnly: true, + ExpectNonEmptyPlan: true, + }, + // Then re-create and make sure it is there again. + { + Config: testAccBBPulsarJobBasic(jobName, appid), + Check: testAccCheckPulsarJobExists("ns1_pulsarjob.it", &job), + }, + }, + }) +} + +func testAccJSPulsarJobBasic(jobName string, appid string) string { + return fmt.Sprintf(`resource "ns1_pulsarjob" "it" { + name = "%s" + typeid = "latency" + appid = "%s" + config = { + host = "testAccHost" + url_path = "/testAccURLPath" + } +} +`, jobName, appid) +} + +func testAccJSPulsarJobUpdated(jobName string, appid string) string { + return fmt.Sprintf(`resource "ns1_pulsarjob" "it" { + name = "%s" + typeid = "latency" + appid = "%s" + active = false + shared = false + config = { + host = "testAccUpdatedHost" + url_path = "/testAccUpdatedURLPath" + } +} +`, jobName, appid) +} + +func testAccJSPulsarJobBlendMetricWeights(jobName string, appid string) string { + return fmt.Sprintf(`resource "ns1_pulsarjob" "it" { + name = "%s" + typeid = "latency" + appid = "%s" + config = { + host = "testAccCompleteHost" + url_path = "/testAccCompleteURLPath" + } + blend_metric_weights = { + timestamp = 123 + } + weights { + name = "testAccWeight1" + weight = 123 + default_value = 12.3 + maximize = false + } + weights { + name = "testAccWeight2" + weight = 321 + default_value = 32.1 + maximize = true + } +} +`, jobName, appid) +} + +func testAccBBPulsarJobBasic(jobName string, appid string) string { + return fmt.Sprintf(`resource "ns1_pulsarjob" "it" { + name = "%s" + typeid = "custom" + appid = "%s" +} +`, jobName, appid) +} + +func testAccBBPulsarJobUpdated(jobName string, appid string) string { + return fmt.Sprintf(`resource "ns1_pulsarjob" "it" { + name = "%s" + typeid = "custom" + appid = "%s" + active = false + shared = false +} +`, jobName, appid) +} + +func testAccBBPulsarJobConverted(jobName string, appid string) string { + return fmt.Sprintf(`resource "ns1_pulsarjob" "it" { + name = "%s" + typeid = "custom" + appid = "%s" + config = { + host = "" + url_path = "" + } +} +`, jobName, appid) +} + +func testAccBBPulsarJobBlendMetricWeights(jobName string, appid string) string { + return fmt.Sprintf(`resource "ns1_pulsarjob" "it" { + name = "%s" + typeid = "custom" + appid = "%s" + config = { + host = "testAccHost" + url_path = "/testAccUrlPath" + } + blend_metric_weights = { + timestamp = 123 + } + weights { + name = "testAccWeight1" + weight = 123 + default_value = 12.3 + maximize = false + } + weights { + name = "testAccWeight2" + weight = 321 + default_value = 32.1 + maximize = true + } +} +`, jobName, appid) +} + +func testAccCheckPulsarJobExists(n string, job *pulsar.PulsarJob) 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 ID is set") + } + + client := testAccProvider.Meta().(*ns1.Client) + + foundPulsarJob, _, err := client.PulsarJobs.Get(rs.Primary.Attributes["appid"], rs.Primary.ID) + + p := rs.Primary + + if err != nil { + return err + } + + if foundPulsarJob.JobID != p.Attributes["id"] { + return fmt.Errorf("pulsar job not found") + } + + *job = *foundPulsarJob + + return nil + } +} + +func testAccCheckPulsarJobDestroy(s *terraform.State) error { + client := testAccProvider.Meta().(*ns1.Client) + + for _, rs := range s.RootModule().Resources { + if rs.Type != "ns1_pulsarjob" { + continue + } + + pulsarJob, _, err := client.PulsarJobs.Get(rs.Primary.Attributes["appid"], rs.Primary.ID) + + if err == nil { + return fmt.Errorf("pulsar job still exists: %#v: %#v", err, pulsarJob) + } + + } + + return nil +} + +func testAccCheckPulsarJobName(job *pulsar.PulsarJob, expected string) resource.TestCheckFunc { + return func(s *terraform.State) error { + if job.Name != expected { + return fmt.Errorf("job.Name: got: %s want: %s", job.Name, expected) + } + return nil + } +} + +func testAccCheckPulsarJobTypeID(job *pulsar.PulsarJob, expected string) resource.TestCheckFunc { + return func(s *terraform.State) error { + if job.TypeID != expected { + return fmt.Errorf("job.TypeID: got: %s want: %s", job.TypeID, expected) + } + return nil + } +} + +func testAccCheckPulsarJobAppID(job *pulsar.PulsarJob, expected string) resource.TestCheckFunc { + return func(s *terraform.State) error { + if job.AppID != expected { + return fmt.Errorf("job.AppID: got: %s want: %s", job.AppID, expected) + } + return nil + } +} + +func testAccCheckPulsarJobActive(job *pulsar.PulsarJob, expected bool) resource.TestCheckFunc { + return func(s *terraform.State) error { + if job.Active != expected { + return fmt.Errorf("job.Active: got: %t want: %t", job.Active, expected) + } + return nil + } +} + +func testAccCheckPulsarJobShared(job *pulsar.PulsarJob, expected bool) resource.TestCheckFunc { + return func(s *terraform.State) error { + if job.Shared != expected { + return fmt.Errorf("job.Shared: got: %t want: %t", job.Shared, expected) + } + return nil + } +} + +func testAccCheckPulsarJobSCommunity(job *pulsar.PulsarJob, expected bool) resource.TestCheckFunc { + return func(s *terraform.State) error { + if job.Community != expected { + return fmt.Errorf("job.Community: got: %t want: %t", job.Community, expected) + } + return nil + } +} + +func testAccCheckPulsarJobSHost(job *pulsar.PulsarJob, expected string) resource.TestCheckFunc { + return func(s *terraform.State) error { + if *job.Config.Host != expected { + return fmt.Errorf("job.Config.Host: got: %s want: %s", *job.Config.Host, expected) + } + return nil + } +} + +func testAccCheckPulsarJobSUrlPath(job *pulsar.PulsarJob, expected string) resource.TestCheckFunc { + return func(s *terraform.State) error { + if *job.Config.URL_Path != expected { + return fmt.Errorf("job.Config.URL_Path: got: %s want: %s", *job.Config.URL_Path, expected) + } + return nil + } +} + +func testAccCHeckPulsarJobBlendMetricWeights_timestamp(job *pulsar.PulsarJob, expected int) resource.TestCheckFunc { + return func(s *terraform.State) error { + if job.Config.BlendMetricWeights.Timestamp != expected { + return fmt.Errorf("job.Config.BlendMetricWeights.Timestamp: got: %v want: %v", job.Config.BlendMetricWeights.Timestamp, expected) + } + return nil + } +} + +func testAccCHeckPulsarJobBlendMetricWeights_weights(job *pulsar.PulsarJob, expected []pulsar.Weights) resource.TestCheckFunc { + return func(s *terraform.State) error { + numberWeights := len(job.Config.BlendMetricWeights.Weights) + numberExpected := len(expected) + if numberWeights != numberExpected { + return fmt.Errorf("job.Config.BlendMetricWeights.Weights: got: %v elements want: %v elements", numberWeights, numberExpected) + } + + for i, weight := range job.Config.BlendMetricWeights.Weights { + if weight.Name != expected[i].Name { + return fmt.Errorf("job.Config.BlendMetricWeights.Weights[%v].Name: got: %s want: %s", i, weight.Name, expected[i].Name) + } + if weight.Weight != expected[i].Weight { + return fmt.Errorf("job.Config.BlendMetricWeights.Weights[%v].Weight: got: %v want: %v", i, weight.Weight, expected[i].Weight) + } + if weight.DefaultValue != expected[i].DefaultValue { + return fmt.Errorf("job.Config.BlendMetricWeights.Weights[%v].DefaultValue: got: %v want: %v", i, weight.DefaultValue, expected[i].DefaultValue) + } + if weight.Maximize != expected[i].Maximize { + return fmt.Errorf("job.Config.BlendMetricWeights.Weights[%v].Maximize: got: %v want: %v", i, weight.Maximize, expected[i].Maximize) + } + } + return nil + } +} + +func testAccManualDeletePulsarJob(job *pulsar.PulsarJob) func() { + return func() { + client := testAccProvider.Meta().(*ns1.Client) + _, err := client.PulsarJobs.Delete(job) + // Not a big deal if this fails, it will get caught in the test conditions and fail the test. + if err != nil { + log.Printf("failed to delete pulsar job: %v", err) + } + } +} diff --git a/website/docs/r/pulsar_job.html .markdown b/website/docs/r/pulsar_job.html .markdown new file mode 100644 index 00000000..5194c2e9 --- /dev/null +++ b/website/docs/r/pulsar_job.html .markdown @@ -0,0 +1,65 @@ +--- +layout: "ns1" +page_title: "NS1: pulsarjob" +sidebar_current: "docs-ns1-resource-pulsarjob" +description: |- + Provides a Pulsar Job resource. +--- + +# ns1\_pulsar\_job + +Provides a Pulsar job resource. This can be used to create, modify, and delete zones. + +## Example Usage + +```hcl +# Create a new Pulsar Application +resource "ns1_application" "example" { + name = "terraform.example.io" +} + +# Create a new Pulsar JavaScript Job with Blend Metric Weights and multiple weights +resource "ns1_pulsarjob" "example_javascript" { + name = "terraform.example_javascript.io" + appid = "${ns1_pulsar_application.example.id}" + typeid = "latency" + + config = { + host = "terraform.job_host.io" + url_path = "/terraform.job_url_path.io" + } +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) Name of the Pulsar job. Typically, this is the name of the CDN or endpoint. +* `appid` - (Required) ID of the Pulsar app. +* `typeid` - (Required) Specifies the type of Pulsar job - either latency or custom. +* `active` - (Optional) The job's status, if it's active or not. +* `shared` - (Optional) Enable to share data with other approved accounts. +* `Config` - (Optional) [Config](#config-1) is documented below. Note: **Required if typeid is "latency"** + + +#### Config + +`config` support several sub-options related to job configuration: + +* `host` - (Required) Hostname where the resource is located. +* `url_path` - (Required) URL path to be appended to the host. +* `https` - (Optional) Indicates whether or not to use HTTPS in measurements. +* `http` - (Optional) Indicates whether or not to use HTTP in measurements. +* `request_timeout_millis` - (Optional) The amount of time to allow a single job to perform N runs. +* `job_timeout_millis` - (Optional) The amount of time to allow a single job to perform 1 run. +* `use_xhr` - (Optional) Indicates wheter or not to use XmlHttpRequest (XHR) when taking measurements. +* `satuc_values` - (Optional) Indicates wheter or not to skip aggregation for this job's measurements. + +## Import + +`terraform import ns1_pulsarjob. _` + +## NS1 Documentation + +[Pulsar Job Api Docs](https://ns1.com/api#jobs) From 4a5be1efdb44cee49414055bb455a13095c71237 Mon Sep 17 00:00:00 2001 From: Thiago Uehara Date: Tue, 31 Aug 2021 18:32:01 -0300 Subject: [PATCH 3/3] Small changes from PR comments --- go.mod | 2 +- go.sum | 4 +- ns1/resource_pulsar_job.go | 39 +++---- ns1/resource_pulsar_job_test.go | 138 +++++++++++------------ website/docs/r/pulsar_job.html .markdown | 14 +-- 5 files changed, 99 insertions(+), 98 deletions(-) diff --git a/go.mod b/go.mod index 77f93f1f..50c30512 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/stretchr/objx v0.2.0 // indirect github.com/stretchr/testify v1.4.0 gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect - gopkg.in/ns1/ns1-go.v2 v2.6.2 + gopkg.in/ns1/ns1-go.v2 v2.6.3 gopkg.in/yaml.v2 v2.2.7 // indirect ) diff --git a/go.sum b/go.sum index 501c03f9..8bd30b01 100644 --- a/go.sum +++ b/go.sum @@ -278,8 +278,8 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/cheggaaa/pb.v1 v1.0.27/go.mod h1:V/YB90LKu/1FcN3WVnfiiE5oMCibMjukxqG/qStrOgw= -gopkg.in/ns1/ns1-go.v2 v2.6.2 h1:tC+gRSN6fmnb9l9cVrIysXyuRO0wV6cZrjDqlMB0gGc= -gopkg.in/ns1/ns1-go.v2 v2.6.2/go.mod h1:GMnKY+ZuoJ+lVLL+78uSTjwTz2jMazq6AfGKQOYhsPk= +gopkg.in/ns1/ns1-go.v2 v2.6.3 h1:VqMsosssl8WQrN5aWSK+jx4UjHiKPjAkyOJycdiZMZo= +gopkg.in/ns1/ns1-go.v2 v2.6.3/go.mod h1:GMnKY+ZuoJ+lVLL+78uSTjwTz2jMazq6AfGKQOYhsPk= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo= gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/ns1/resource_pulsar_job.go b/ns1/resource_pulsar_job.go index 56233617..22cba9a3 100644 --- a/ns1/resource_pulsar_job.go +++ b/ns1/resource_pulsar_job.go @@ -1,6 +1,7 @@ package ns1 import ( + "errors" "fmt" "log" "strconv" @@ -22,7 +23,7 @@ func pulsarJobResource() *schema.Resource { Type: schema.TypeString, Required: true, }, - "typeid": { + "type_id": { Type: schema.TypeString, Required: true, ValidateFunc: validateTypeId, @@ -31,14 +32,14 @@ func pulsarJobResource() *schema.Resource { Type: schema.TypeBool, Computed: true, }, - "jobid": { + "job_id": { Type: schema.TypeString, Computed: true, ForceNew: true, }, - "appid": { + "app_id": { Type: schema.TypeString, - Optional: true, + Required: true, }, "active": { Type: schema.TypeBool, @@ -48,7 +49,7 @@ func pulsarJobResource() *schema.Resource { "shared": { Type: schema.TypeBool, Optional: true, - Default: true, + Default: false, }, "config": { Type: schema.TypeMap, @@ -145,9 +146,9 @@ func pulsarJobToResourceData(d *schema.ResourceData, j *pulsar.PulsarJob) error d.SetId(j.JobID) d.Set("customer", j.Customer) d.Set("name", j.Name) - d.Set("typeid", j.TypeID) + d.Set("type_id", j.TypeID) d.Set("community", j.Community) - d.Set("appid", j.AppID) + d.Set("app_id", j.AppID) d.Set("active", j.Active) d.Set("shared", j.Shared) @@ -228,9 +229,9 @@ func weightsToResourceData(w *pulsar.Weights) map[string]interface{} { func resourceDataToPulsarJob(j *pulsar.PulsarJob, d *schema.ResourceData) error { j.Name = d.Get("name").(string) - j.TypeID = d.Get("typeid").(string) + j.TypeID = d.Get("type_id").(string) j.JobID = d.Id() - j.AppID = d.Get("appid").(string) + j.AppID = d.Get("app_id").(string) j.Active = d.Get("active").(bool) j.Shared = d.Get("shared").(bool) @@ -369,16 +370,16 @@ func PulsarJobCreate(d *schema.ResourceData, meta interface{}) error { // pulsarJobRead reads the given zone data from ns1 func pulsarJobRead(d *schema.ResourceData, meta interface{}) error { client := meta.(*ns1.Client) - j, resp, err := client.PulsarJobs.Get(d.Get("appid").(string), d.Id()) + j, resp, err := client.PulsarJobs.Get(d.Get("app_id").(string), d.Id()) if err != nil { - if err == ns1.ErrAppMissing { - log.Printf("[DEBUG] NS1 Pulsar Application (%s) not found", d.Get("appid")) + if errors.Is(err, ns1.ErrAppMissing) { + log.Printf("[DEBUG] NS1 Pulsar Application (%s) not found", d.Get("app_id")) d.SetId("") return nil } - if err == ns1.ErrJobMissing { - log.Printf("[DEBUG] NS1 Pulsar Job (%s) not found", d.Get("jobid")) + if errors.Is(err, ns1.ErrJobMissing) { + log.Printf("[DEBUG] NS1 Pulsar Job (%s) not found", d.Get("job_id")) d.SetId("") return nil } @@ -396,7 +397,7 @@ func PulsarJobUpdate(job_schema *schema.ResourceData, meta interface{}) error { client := meta.(*ns1.Client) j := pulsar.PulsarJob{ JobID: job_schema.Id(), - AppID: job_schema.Get("appid").(string), + AppID: job_schema.Get("app_id").(string), } if err := resourceDataToPulsarJob(&j, job_schema); err != nil { return err @@ -424,7 +425,7 @@ func validateTypeId(val interface{}, key string) (warns []string, errs []error) if v != "latency" && v != "custom" { errs = append(errs, fmt.Errorf( - "typeid %s invalid, please select between 'latency' or 'custom'", v, + "type_id %s invalid, please select between 'latency' or 'custom'", v, ), ) } @@ -434,11 +435,11 @@ func validateTypeId(val interface{}, key string) (warns []string, errs []error) func pulsarJobImportStateFunc(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { parts := strings.Split(d.Id(), "_") if len(parts) != 2 { - return nil, fmt.Errorf("invalid job specifier. Expected 2 ids (\"appid_jobid\", got %d)", len(parts)) + return nil, fmt.Errorf("invalid job specifier. Expected 2 ids (\"app_id\"_\"job_id\", got %d)", len(parts)) } - d.Set("appid", parts[0]) - d.Set("jobid", parts[1]) + d.Set("app_id", parts[0]) + d.Set("job_id", parts[1]) d.SetId(parts[1]) return []*schema.ResourceData{d}, nil diff --git a/ns1/resource_pulsar_job_test.go b/ns1/resource_pulsar_job_test.go index f383edcb..0d610f25 100644 --- a/ns1/resource_pulsar_job_test.go +++ b/ns1/resource_pulsar_job_test.go @@ -18,7 +18,7 @@ func TestAccPulsarJob_basic(t *testing.T) { var ( job = pulsar.PulsarJob{} jobName = fmt.Sprintf("terraform-test-%s.io", acctest.RandStringFromCharSet(15, acctest.CharSetAlphaNum)) - appid = "yv5kfn" + app_id = "yv5kfn" ) // Basic test for JavaScript jobs resource.Test(t, resource.TestCase{ @@ -27,14 +27,14 @@ func TestAccPulsarJob_basic(t *testing.T) { CheckDestroy: testAccCheckPulsarJobDestroy, Steps: []resource.TestStep{ { - Config: testAccJSPulsarJobBasic(jobName, appid), + Config: testAccJSPulsarJobBasic(jobName, app_id), Check: resource.ComposeTestCheckFunc( testAccCheckPulsarJobExists("ns1_pulsarjob.it", &job), testAccCheckPulsarJobName(&job, jobName), testAccCheckPulsarJobTypeID(&job, "latency"), - testAccCheckPulsarJobAppID(&job, appid), + testAccCheckPulsarJobAppID(&job, app_id), testAccCheckPulsarJobActive(&job, true), - testAccCheckPulsarJobShared(&job, true), + testAccCheckPulsarJobShared(&job, false), testAccCheckPulsarJobSCommunity(&job, false), testAccCheckPulsarJobSHost(&job, "testAccHost"), testAccCheckPulsarJobSUrlPath(&job, "/testAccURLPath"), @@ -50,14 +50,14 @@ func TestAccPulsarJob_basic(t *testing.T) { CheckDestroy: testAccCheckPulsarJobDestroy, Steps: []resource.TestStep{ { - Config: testAccBBPulsarJobBasic(jobName, appid), + Config: testAccBBPulsarJobBasic(jobName, app_id), Check: resource.ComposeTestCheckFunc( testAccCheckPulsarJobExists("ns1_pulsarjob.it", &job), testAccCheckPulsarJobName(&job, jobName), testAccCheckPulsarJobTypeID(&job, "custom"), testAccCheckPulsarJobAppID(&job, "yv5kfn"), testAccCheckPulsarJobActive(&job, true), - testAccCheckPulsarJobShared(&job, true), + testAccCheckPulsarJobShared(&job, false), testAccCheckPulsarJobSCommunity(&job, false), ), }, @@ -70,7 +70,7 @@ func TestAccPulsarJob_updated_same_type(t *testing.T) { var ( job = pulsar.PulsarJob{} jobName = fmt.Sprintf("terraform-test-%s.io", acctest.RandStringFromCharSet(15, acctest.CharSetAlphaNum)) - appid = "yv5kfn" + app_id = "yv5kfn" updatedJobName = fmt.Sprintf("terraform-test-%s.io", acctest.RandStringFromCharSet(15, acctest.CharSetAlphaNum)) ) @@ -81,26 +81,26 @@ func TestAccPulsarJob_updated_same_type(t *testing.T) { CheckDestroy: testAccCheckPulsarJobDestroy, Steps: []resource.TestStep{ { - Config: testAccJSPulsarJobBasic(jobName, appid), + Config: testAccJSPulsarJobBasic(jobName, app_id), Check: resource.ComposeTestCheckFunc( testAccCheckPulsarJobExists("ns1_pulsarjob.it", &job), testAccCheckPulsarJobName(&job, jobName), testAccCheckPulsarJobTypeID(&job, "latency"), - testAccCheckPulsarJobAppID(&job, appid), + testAccCheckPulsarJobAppID(&job, app_id), testAccCheckPulsarJobActive(&job, true), - testAccCheckPulsarJobShared(&job, true), + testAccCheckPulsarJobShared(&job, false), testAccCheckPulsarJobSCommunity(&job, false), testAccCheckPulsarJobSHost(&job, "testAccHost"), testAccCheckPulsarJobSUrlPath(&job, "/testAccURLPath"), ), }, { - Config: testAccJSPulsarJobUpdated(updatedJobName, appid), + Config: testAccJSPulsarJobUpdated(updatedJobName, app_id), Check: resource.ComposeTestCheckFunc( testAccCheckPulsarJobExists("ns1_pulsarjob.it", &job), testAccCheckPulsarJobName(&job, updatedJobName), testAccCheckPulsarJobTypeID(&job, "latency"), - testAccCheckPulsarJobAppID(&job, appid), + testAccCheckPulsarJobAppID(&job, app_id), testAccCheckPulsarJobActive(&job, false), testAccCheckPulsarJobShared(&job, false), testAccCheckPulsarJobSCommunity(&job, false), @@ -118,19 +118,19 @@ func TestAccPulsarJob_updated_same_type(t *testing.T) { CheckDestroy: testAccCheckPulsarJobDestroy, Steps: []resource.TestStep{ { - Config: testAccBBPulsarJobBasic(jobName, appid), + Config: testAccBBPulsarJobBasic(jobName, app_id), Check: resource.ComposeTestCheckFunc( testAccCheckPulsarJobExists("ns1_pulsarjob.it", &job), testAccCheckPulsarJobName(&job, jobName), testAccCheckPulsarJobTypeID(&job, "custom"), testAccCheckPulsarJobAppID(&job, "yv5kfn"), testAccCheckPulsarJobActive(&job, true), - testAccCheckPulsarJobShared(&job, true), + testAccCheckPulsarJobShared(&job, false), testAccCheckPulsarJobSCommunity(&job, false), ), }, { - Config: testAccBBPulsarJobUpdated(updatedJobName, appid), + Config: testAccBBPulsarJobUpdated(updatedJobName, app_id), Check: resource.ComposeTestCheckFunc( testAccCheckPulsarJobExists("ns1_pulsarjob.it", &job), testAccCheckPulsarJobName(&job, updatedJobName), @@ -150,7 +150,7 @@ func TestAccPulsarJob_updated_different_type(t *testing.T) { var ( job = pulsar.PulsarJob{} jobName = fmt.Sprintf("terraform-test-%s.io", acctest.RandStringFromCharSet(15, acctest.CharSetAlphaNum)) - appid = "yv5kfn" + app_id = "yv5kfn" updatedJobName = fmt.Sprintf("terraform-test-%s.io", acctest.RandStringFromCharSet(15, acctest.CharSetAlphaNum)) ) @@ -162,28 +162,28 @@ func TestAccPulsarJob_updated_different_type(t *testing.T) { CheckDestroy: testAccCheckPulsarJobDestroy, Steps: []resource.TestStep{ { - Config: testAccJSPulsarJobBasic(jobName, appid), + Config: testAccJSPulsarJobBasic(jobName, app_id), Check: resource.ComposeTestCheckFunc( testAccCheckPulsarJobExists("ns1_pulsarjob.it", &job), testAccCheckPulsarJobName(&job, jobName), testAccCheckPulsarJobTypeID(&job, "latency"), - testAccCheckPulsarJobAppID(&job, appid), + testAccCheckPulsarJobAppID(&job, app_id), testAccCheckPulsarJobActive(&job, true), - testAccCheckPulsarJobShared(&job, true), + testAccCheckPulsarJobShared(&job, false), testAccCheckPulsarJobSCommunity(&job, false), testAccCheckPulsarJobSHost(&job, "testAccHost"), testAccCheckPulsarJobSUrlPath(&job, "/testAccURLPath"), ), }, { - Config: testAccBBPulsarJobConverted(updatedJobName, appid), + Config: testAccBBPulsarJobConverted(updatedJobName, app_id), Check: resource.ComposeTestCheckFunc( testAccCheckPulsarJobExists("ns1_pulsarjob.it", &job), testAccCheckPulsarJobName(&job, updatedJobName), testAccCheckPulsarJobTypeID(&job, "custom"), testAccCheckPulsarJobAppID(&job, "yv5kfn"), testAccCheckPulsarJobActive(&job, true), - testAccCheckPulsarJobShared(&job, true), + testAccCheckPulsarJobShared(&job, false), testAccCheckPulsarJobSCommunity(&job, false), ), }, @@ -197,24 +197,24 @@ func TestAccPulsarJob_updated_different_type(t *testing.T) { CheckDestroy: testAccCheckPulsarJobDestroy, Steps: []resource.TestStep{ { - Config: testAccBBPulsarJobBasic(jobName, appid), + Config: testAccBBPulsarJobBasic(jobName, app_id), Check: resource.ComposeTestCheckFunc( testAccCheckPulsarJobExists("ns1_pulsarjob.it", &job), testAccCheckPulsarJobName(&job, jobName), testAccCheckPulsarJobTypeID(&job, "custom"), testAccCheckPulsarJobAppID(&job, "yv5kfn"), testAccCheckPulsarJobActive(&job, true), - testAccCheckPulsarJobShared(&job, true), + testAccCheckPulsarJobShared(&job, false), testAccCheckPulsarJobSCommunity(&job, false), ), }, { - Config: testAccJSPulsarJobUpdated(updatedJobName, appid), + Config: testAccJSPulsarJobUpdated(updatedJobName, app_id), Check: resource.ComposeTestCheckFunc( testAccCheckPulsarJobExists("ns1_pulsarjob.it", &job), testAccCheckPulsarJobName(&job, updatedJobName), testAccCheckPulsarJobTypeID(&job, "latency"), - testAccCheckPulsarJobAppID(&job, appid), + testAccCheckPulsarJobAppID(&job, app_id), testAccCheckPulsarJobActive(&job, false), testAccCheckPulsarJobShared(&job, false), testAccCheckPulsarJobSCommunity(&job, false), @@ -231,7 +231,7 @@ func TestAccPulsarJob_BlendMetricWeights(t *testing.T) { var ( job = pulsar.PulsarJob{} jobName = fmt.Sprintf("terraform-test-%s.io", acctest.RandStringFromCharSet(15, acctest.CharSetAlphaNum)) - appid = "yv5kfn" + app_id = "yv5kfn" weights = []pulsar.Weights{ { @@ -256,14 +256,14 @@ func TestAccPulsarJob_BlendMetricWeights(t *testing.T) { CheckDestroy: testAccCheckPulsarJobDestroy, Steps: []resource.TestStep{ { - Config: testAccJSPulsarJobBlendMetricWeights(jobName, appid), + Config: testAccJSPulsarJobBlendMetricWeights(jobName, app_id), Check: resource.ComposeTestCheckFunc( testAccCheckPulsarJobExists("ns1_pulsarjob.it", &job), testAccCheckPulsarJobName(&job, jobName), testAccCheckPulsarJobTypeID(&job, "latency"), - testAccCheckPulsarJobAppID(&job, appid), + testAccCheckPulsarJobAppID(&job, app_id), testAccCheckPulsarJobActive(&job, true), - testAccCheckPulsarJobShared(&job, true), + testAccCheckPulsarJobShared(&job, false), testAccCheckPulsarJobSCommunity(&job, false), testAccCheckPulsarJobSHost(&job, "testAccCompleteHost"), testAccCheckPulsarJobSUrlPath(&job, "/testAccCompleteURLPath"), @@ -281,16 +281,16 @@ func TestAccPulsarJob_BlendMetricWeights(t *testing.T) { CheckDestroy: testAccCheckPulsarJobDestroy, Steps: []resource.TestStep{ { - Config: testAccBBPulsarJobBlendMetricWeights(jobName, appid), + Config: testAccBBPulsarJobBlendMetricWeights(jobName, app_id), Check: resource.ComposeTestCheckFunc( testAccCheckPulsarJobExists("ns1_pulsarjob.it", &job), testAccCheckPulsarJobName(&job, jobName), testAccCheckPulsarJobTypeID(&job, "custom"), testAccCheckPulsarJobSHost(&job, "testAccHost"), testAccCheckPulsarJobSUrlPath(&job, "/testAccUrlPath"), - testAccCheckPulsarJobAppID(&job, appid), + testAccCheckPulsarJobAppID(&job, app_id), testAccCheckPulsarJobActive(&job, true), - testAccCheckPulsarJobShared(&job, true), + testAccCheckPulsarJobShared(&job, false), testAccCheckPulsarJobSCommunity(&job, false), testAccCHeckPulsarJobBlendMetricWeights_timestamp(&job, 123), testAccCHeckPulsarJobBlendMetricWeights_weights(&job, weights), @@ -305,7 +305,7 @@ func TestAccPulsarJob_ManualDelete(t *testing.T) { var ( job = pulsar.PulsarJob{} jobName = fmt.Sprintf("terraform-test-%s.io", acctest.RandStringFromCharSet(15, acctest.CharSetAlphaNum)) - appid = "yv5kfn" + app_id = "yv5kfn" ) // Manual deletion test for JavaScript jobs resource.Test(t, resource.TestCase{ @@ -314,7 +314,7 @@ func TestAccPulsarJob_ManualDelete(t *testing.T) { CheckDestroy: testAccCheckPulsarJobDestroy, Steps: []resource.TestStep{ { - Config: testAccJSPulsarJobBasic(jobName, appid), + Config: testAccJSPulsarJobBasic(jobName, app_id), Check: resource.ComposeTestCheckFunc( testAccCheckPulsarJobExists("ns1_pulsarjob.it", &job), ), @@ -322,13 +322,13 @@ func TestAccPulsarJob_ManualDelete(t *testing.T) { // Simulate a manual deletion of the pulsar job and verify that the plan has a diff. { PreConfig: testAccManualDeletePulsarJob(&job), - Config: testAccJSPulsarJobBasic(jobName, appid), + Config: testAccJSPulsarJobBasic(jobName, app_id), PlanOnly: true, ExpectNonEmptyPlan: true, }, // Then re-create and make sure it is there again. { - Config: testAccJSPulsarJobBasic(jobName, appid), + Config: testAccJSPulsarJobBasic(jobName, app_id), Check: testAccCheckPulsarJobExists("ns1_pulsarjob.it", &job), }, }, @@ -341,7 +341,7 @@ func TestAccPulsarJob_ManualDelete(t *testing.T) { CheckDestroy: testAccCheckPulsarJobDestroy, Steps: []resource.TestStep{ { - Config: testAccBBPulsarJobBasic(jobName, appid), + Config: testAccBBPulsarJobBasic(jobName, app_id), Check: resource.ComposeTestCheckFunc( testAccCheckPulsarJobExists("ns1_pulsarjob.it", &job), ), @@ -349,37 +349,37 @@ func TestAccPulsarJob_ManualDelete(t *testing.T) { // Simulate a manual deletion of the pulsar job and verify that the plan has a diff. { PreConfig: testAccManualDeletePulsarJob(&job), - Config: testAccBBPulsarJobBasic(jobName, appid), + Config: testAccBBPulsarJobBasic(jobName, app_id), PlanOnly: true, ExpectNonEmptyPlan: true, }, // Then re-create and make sure it is there again. { - Config: testAccBBPulsarJobBasic(jobName, appid), + Config: testAccBBPulsarJobBasic(jobName, app_id), Check: testAccCheckPulsarJobExists("ns1_pulsarjob.it", &job), }, }, }) } -func testAccJSPulsarJobBasic(jobName string, appid string) string { +func testAccJSPulsarJobBasic(jobName string, app_id string) string { return fmt.Sprintf(`resource "ns1_pulsarjob" "it" { name = "%s" - typeid = "latency" - appid = "%s" + type_id = "latency" + app_id = "%s" config = { host = "testAccHost" url_path = "/testAccURLPath" } } -`, jobName, appid) +`, jobName, app_id) } -func testAccJSPulsarJobUpdated(jobName string, appid string) string { +func testAccJSPulsarJobUpdated(jobName string, app_id string) string { return fmt.Sprintf(`resource "ns1_pulsarjob" "it" { name = "%s" - typeid = "latency" - appid = "%s" + type_id = "latency" + app_id = "%s" active = false shared = false config = { @@ -387,14 +387,14 @@ func testAccJSPulsarJobUpdated(jobName string, appid string) string { url_path = "/testAccUpdatedURLPath" } } -`, jobName, appid) +`, jobName, app_id) } -func testAccJSPulsarJobBlendMetricWeights(jobName string, appid string) string { +func testAccJSPulsarJobBlendMetricWeights(jobName string, app_id string) string { return fmt.Sprintf(`resource "ns1_pulsarjob" "it" { name = "%s" - typeid = "latency" - appid = "%s" + type_id = "latency" + app_id = "%s" config = { host = "testAccCompleteHost" url_path = "/testAccCompleteURLPath" @@ -415,47 +415,47 @@ func testAccJSPulsarJobBlendMetricWeights(jobName string, appid string) string { maximize = true } } -`, jobName, appid) +`, jobName, app_id) } -func testAccBBPulsarJobBasic(jobName string, appid string) string { +func testAccBBPulsarJobBasic(jobName string, app_id string) string { return fmt.Sprintf(`resource "ns1_pulsarjob" "it" { name = "%s" - typeid = "custom" - appid = "%s" + type_id = "custom" + app_id = "%s" } -`, jobName, appid) +`, jobName, app_id) } -func testAccBBPulsarJobUpdated(jobName string, appid string) string { +func testAccBBPulsarJobUpdated(jobName string, app_id string) string { return fmt.Sprintf(`resource "ns1_pulsarjob" "it" { name = "%s" - typeid = "custom" - appid = "%s" + type_id = "custom" + app_id = "%s" active = false shared = false } -`, jobName, appid) +`, jobName, app_id) } -func testAccBBPulsarJobConverted(jobName string, appid string) string { +func testAccBBPulsarJobConverted(jobName string, app_id string) string { return fmt.Sprintf(`resource "ns1_pulsarjob" "it" { name = "%s" - typeid = "custom" - appid = "%s" + type_id = "custom" + app_id = "%s" config = { host = "" url_path = "" } } -`, jobName, appid) +`, jobName, app_id) } -func testAccBBPulsarJobBlendMetricWeights(jobName string, appid string) string { +func testAccBBPulsarJobBlendMetricWeights(jobName string, app_id string) string { return fmt.Sprintf(`resource "ns1_pulsarjob" "it" { name = "%s" - typeid = "custom" - appid = "%s" + type_id = "custom" + app_id = "%s" config = { host = "testAccHost" url_path = "/testAccUrlPath" @@ -476,7 +476,7 @@ func testAccBBPulsarJobBlendMetricWeights(jobName string, appid string) string { maximize = true } } -`, jobName, appid) +`, jobName, app_id) } func testAccCheckPulsarJobExists(n string, job *pulsar.PulsarJob) resource.TestCheckFunc { @@ -493,7 +493,7 @@ func testAccCheckPulsarJobExists(n string, job *pulsar.PulsarJob) resource.TestC client := testAccProvider.Meta().(*ns1.Client) - foundPulsarJob, _, err := client.PulsarJobs.Get(rs.Primary.Attributes["appid"], rs.Primary.ID) + foundPulsarJob, _, err := client.PulsarJobs.Get(rs.Primary.Attributes["app_id"], rs.Primary.ID) p := rs.Primary @@ -519,7 +519,7 @@ func testAccCheckPulsarJobDestroy(s *terraform.State) error { continue } - pulsarJob, _, err := client.PulsarJobs.Get(rs.Primary.Attributes["appid"], rs.Primary.ID) + pulsarJob, _, err := client.PulsarJobs.Get(rs.Primary.Attributes["app_id"], rs.Primary.ID) if err == nil { return fmt.Errorf("pulsar job still exists: %#v: %#v", err, pulsarJob) diff --git a/website/docs/r/pulsar_job.html .markdown b/website/docs/r/pulsar_job.html .markdown index 5194c2e9..ac87dced 100644 --- a/website/docs/r/pulsar_job.html .markdown +++ b/website/docs/r/pulsar_job.html .markdown @@ -21,8 +21,8 @@ resource "ns1_application" "example" { # Create a new Pulsar JavaScript Job with Blend Metric Weights and multiple weights resource "ns1_pulsarjob" "example_javascript" { name = "terraform.example_javascript.io" - appid = "${ns1_pulsar_application.example.id}" - typeid = "latency" + app_id = "${ns1_pulsar_application.example.id}" + type_id = "latency" config = { host = "terraform.job_host.io" @@ -36,11 +36,11 @@ resource "ns1_pulsarjob" "example_javascript" { The following arguments are supported: * `name` - (Required) Name of the Pulsar job. Typically, this is the name of the CDN or endpoint. -* `appid` - (Required) ID of the Pulsar app. -* `typeid` - (Required) Specifies the type of Pulsar job - either latency or custom. +* `app_id` - (Required) ID of the Pulsar app. +* `type_id` - (Required) Specifies the type of Pulsar job - either latency or custom. * `active` - (Optional) The job's status, if it's active or not. * `shared` - (Optional) Enable to share data with other approved accounts. -* `Config` - (Optional) [Config](#config-1) is documented below. Note: **Required if typeid is "latency"** +* `Config` - (Optional) [Config](#config-1) is documented below. Note: **Required if type_id is "latency"** #### Config @@ -54,11 +54,11 @@ The following arguments are supported: * `request_timeout_millis` - (Optional) The amount of time to allow a single job to perform N runs. * `job_timeout_millis` - (Optional) The amount of time to allow a single job to perform 1 run. * `use_xhr` - (Optional) Indicates wheter or not to use XmlHttpRequest (XHR) when taking measurements. -* `satuc_values` - (Optional) Indicates wheter or not to skip aggregation for this job's measurements. +* `static_values` - (Optional) Indicates wheter or not to skip aggregation for this job's measurements. ## Import -`terraform import ns1_pulsarjob. _` +`terraform import ns1_pulsarjob. _` ## NS1 Documentation