Skip to content

Commit

Permalink
Merge pull request #720 from ovh/dev/aamstutz/fix-dedicated-input
Browse files Browse the repository at this point in the history
fix: Add missing autoscaling params to resource ovh_dbaas_logs_input
  • Loading branch information
amstuta authored Sep 11, 2024
2 parents 2dc82b1 + 8fb14ca commit 2cdaea4
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 51 deletions.
37 changes: 29 additions & 8 deletions ovh/resource_dbaas_logs_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,26 @@ func resourceDbaasLogsInputSchema() map[string]*schema.Schema {
Computed: true,
},
"nb_instance": {
Type: schema.TypeInt,
Description: "Number of instance running",
Optional: true,
ConflictsWith: []string{"autoscale"},
},
"autoscale": {
Type: schema.TypeBool,
Description: "Whether the workload is auto-scaled",
Optional: true,
ConflictsWith: []string{"nb_instance"},
},
"min_scale_instance": {
Type: schema.TypeInt,
Description: "Number of instance running",
Description: "Minimum number of instances in auto-scaled mode",
Optional: true,
},
"max_scale_instance": {
Type: schema.TypeInt,
Description: "Maximum number of instances in auto-scaled mode",
Optional: true,
Computed: true,
},

// computed
Expand Down Expand Up @@ -201,6 +217,11 @@ func resourceDbaasLogsInputSchema() map[string]*schema.Schema {
Description: "Input last update",
Computed: true,
},
"current_nb_instance": {
Type: schema.TypeInt,
Description: "Number of instance running (returned by the API)",
Computed: true,
},
}

return schema
Expand All @@ -210,7 +231,7 @@ func resourceDbaasLogsInputImportState(d *schema.ResourceData, meta interface{})
givenID := d.Id()
splitID := strings.SplitN(givenID, "/", 2)
if len(splitID) != 2 {
return nil, fmt.Errorf("Import Id is not service_name/id formatted")
return nil, fmt.Errorf("import ID is not service_name/id formatted")
}
serviceName := splitID[0]
id := splitID[1]
Expand Down Expand Up @@ -387,7 +408,7 @@ func dbaasLogsInputConfigurationUpdate(ctx context.Context, d *schema.ResourceDa
url.PathEscape(id),
)
if err := config.OVHClient.Put(endpoint, flowggerOpts, res); err != nil {
return fmt.Errorf("Error calling Put %s:\n\t %q", endpoint, err)
return fmt.Errorf("error calling Put %s:\n\t %q", endpoint, err)
}
}

Expand All @@ -401,7 +422,7 @@ func dbaasLogsInputConfigurationUpdate(ctx context.Context, d *schema.ResourceDa
url.PathEscape(id),
)
if err := config.OVHClient.Put(endpoint, logstashOpts, res); err != nil {
return fmt.Errorf("Error calling Put %s:\n\t %q", endpoint, err)
return fmt.Errorf("error calling Put %s:\n\t %q", endpoint, err)
}
}

Expand Down Expand Up @@ -486,7 +507,7 @@ func dbaasLogsInputStart(ctx context.Context, d *schema.ResourceData, meta inter
url.PathEscape(id),
)
if err := config.OVHClient.Post(endpoint, nil, res); err != nil {
return fmt.Errorf("Error calling Put %s:\n\t %q", endpoint, err)
return fmt.Errorf("error calling Put %s:\n\t %q", endpoint, err)
}
}

Expand All @@ -497,7 +518,7 @@ func dbaasLogsInputStart(ctx context.Context, d *schema.ResourceData, meta inter
url.PathEscape(id),
)
if err := config.OVHClient.Post(endpoint, nil, res); err != nil {
return fmt.Errorf("Error calling Put %s:\n\t %q", endpoint, err)
return fmt.Errorf("error calling Put %s:\n\t %q", endpoint, err)
}
}

Expand Down Expand Up @@ -538,7 +559,7 @@ func dbaasLogsInputEnd(ctx context.Context, d *schema.ResourceData, meta interfa
url.PathEscape(id),
)
if err := config.OVHClient.Post(endpoint, nil, res); err != nil {
return fmt.Errorf("Error calling Put %s:\n\t %q", endpoint, err)
return fmt.Errorf("error calling Put %s:\n\t %q", endpoint, err)
}

// Wait for operation status
Expand Down
188 changes: 155 additions & 33 deletions ovh/resource_dbaas_logs_input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,75 @@ data "ovh_dbaas_logs_input_engine" "logstash" {
}
resource "ovh_dbaas_logs_output_graylog_stream" "stream" {
service_name = "%s"
title = "%s"
description = "%s"
service_name = "%s"
title = "%s"
description = "%s"
}
resource "ovh_dbaas_logs_input" "input" {
service_name = ovh_dbaas_logs_output_graylog_stream.stream.service_name
description = ovh_dbaas_logs_output_graylog_stream.stream.description
title = ovh_dbaas_logs_output_graylog_stream.stream.title
engine_id = data.ovh_dbaas_logs_input_engine.logstash.id
stream_id = ovh_dbaas_logs_output_graylog_stream.stream.id
allowed_networks = ["10.0.0.0/16"]
exposed_port = "6154"
nb_instance = 2
configuration {
logstash {
input_section = <<EOF
beats {
port => 6514
ssl => true
ssl_certificate => "/etc/ssl/private/server.crt"
ssl_key => "/etc/ssl/private/server.key"
service_name = ovh_dbaas_logs_output_graylog_stream.stream.service_name
description = ovh_dbaas_logs_output_graylog_stream.stream.description
title = ovh_dbaas_logs_output_graylog_stream.stream.title
engine_id = data.ovh_dbaas_logs_input_engine.logstash.id
stream_id = ovh_dbaas_logs_output_graylog_stream.stream.id
allowed_networks = ["10.0.0.0/16"]
exposed_port = "6154"
nb_instance = %d
configuration {
logstash {
input_section = <<EOF
beats {
port => 6514
ssl => true
ssl_certificate => "/etc/ssl/private/server.crt"
ssl_key => "/etc/ssl/private/server.key"
}
EOF
}
}
}
EOF
`

}
}
const testAccResourceDbaasLogsInput_updated = `
data "ovh_dbaas_logs_input_engine" "logstash" {
service_name = "%s"
name = "%s"
version = "%s"
}
resource "ovh_dbaas_logs_output_graylog_stream" "stream" {
service_name = "%s"
title = "%s"
description = "%s"
}
resource "ovh_dbaas_logs_input" "input" {
service_name = ovh_dbaas_logs_output_graylog_stream.stream.service_name
description = ovh_dbaas_logs_output_graylog_stream.stream.description
title = ovh_dbaas_logs_output_graylog_stream.stream.title
engine_id = data.ovh_dbaas_logs_input_engine.logstash.id
stream_id = ovh_dbaas_logs_output_graylog_stream.stream.id
allowed_networks = ["10.0.0.0/16"]
exposed_port = "6154"
autoscale = true
min_scale_instance = 2
max_scale_instance = 4
configuration {
logstash {
input_section = <<EOF
beats {
port => 6514
ssl => true
ssl_certificate => "/etc/ssl/private/server.crt"
ssl_key => "/etc/ssl/private/server.key"
}
EOF
}
}
}
`

Expand Down Expand Up @@ -161,15 +201,12 @@ func TestAccResourceDbaasLogsInput_basic(t *testing.T) {
title := acctest.RandomWithPrefix(test_prefix)
desc := acctest.RandomWithPrefix(test_prefix)

config := fmt.Sprintf(
testAccResourceDbaasLogsInput_basic,
serviceName,
name,
version,
serviceName,
title,
desc,
)
config := fmt.Sprintf(testAccResourceDbaasLogsInput_basic,
serviceName, name, version, serviceName, title, desc, 2)
configWithMoreInstances := fmt.Sprintf(testAccResourceDbaasLogsInput_basic,
serviceName, name, version, serviceName, title, desc, 4)
configUpdated := fmt.Sprintf(testAccResourceDbaasLogsInput_updated,
serviceName, name, version, serviceName, title, desc)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckDbaasLogsInput(t) },
Expand All @@ -189,6 +226,91 @@ func TestAccResourceDbaasLogsInput_basic(t *testing.T) {
"title",
title,
),
resource.TestCheckResourceAttr(
"ovh_dbaas_logs_input.input",
"autoscale",
"false",
),
resource.TestCheckResourceAttr(
"ovh_dbaas_logs_input.input",
"nb_instance",
"2",
),
resource.TestCheckResourceAttr(
"ovh_dbaas_logs_input.input",
"current_nb_instance",
"2",
),
),
},
{
Config: configWithMoreInstances,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"ovh_dbaas_logs_input.input",
"description",
desc,
),
resource.TestCheckResourceAttr(
"ovh_dbaas_logs_input.input",
"title",
title,
),
resource.TestCheckResourceAttr(
"ovh_dbaas_logs_input.input",
"autoscale",
"false",
),
resource.TestCheckResourceAttr(
"ovh_dbaas_logs_input.input",
"nb_instance",
"4",
),
resource.TestCheckResourceAttr(
"ovh_dbaas_logs_input.input",
"current_nb_instance",
"4",
),
),
},
{
Config: configUpdated,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"ovh_dbaas_logs_input.input",
"description",
desc,
),
resource.TestCheckResourceAttr(
"ovh_dbaas_logs_input.input",
"title",
title,
),
resource.TestCheckResourceAttr(
"ovh_dbaas_logs_input.input",
"autoscale",
"true",
),
resource.TestCheckResourceAttr(
"ovh_dbaas_logs_input.input",
"min_scale_instance",
"2",
),
resource.TestCheckResourceAttr(
"ovh_dbaas_logs_input.input",
"max_scale_instance",
"4",
),
resource.TestCheckResourceAttr(
"ovh_dbaas_logs_input.input",
"nb_instance",
"0",
),
resource.TestCheckResourceAttr(
"ovh_dbaas_logs_input.input",
"current_nb_instance",
"4",
),
),
},
},
Expand Down
37 changes: 28 additions & 9 deletions ovh/types_dbaas_logs_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ type DbaasLogsInput struct {
InputId string `json:"inputId"`
IsRestartRequired bool `json:"isRestartRequired"`
NbInstance *int64 `json:"nbInstance"`
Autoscale bool `json:"autoscale"`
MaxScaleInstance *int `json:"maxScaleInstance"`
MinScaleInstance *int `json:"minScaleInstance"`
PublicAddress string `json:"publicAddress"`
SslCertificate string `json:"sslCertificate"`
Status string `json:"status"`
Expand All @@ -40,6 +43,7 @@ func (v DbaasLogsInput) ToMap() map[string]interface{} {
obj["stream_id"] = v.StreamId
obj["title"] = v.Title
obj["updated_at"] = v.UpdatedAt
obj["autoscale"] = v.Autoscale

if v.AllowedNetworks != nil {
obj["allowed_networks"] = v.AllowedNetworks
Expand All @@ -48,20 +52,29 @@ func (v DbaasLogsInput) ToMap() map[string]interface{} {
obj["exposed_port"] = *v.ExposedPort
}
if v.NbInstance != nil {
obj["nb_instance"] = *v.NbInstance
obj["current_nb_instance"] = *v.NbInstance
}
if v.MaxScaleInstance != nil {
obj["max_scale_instance"] = *v.MaxScaleInstance
}
if v.MinScaleInstance != nil {
obj["min_scale_instance"] = *v.MinScaleInstance
}

return obj
}

type DbaasLogsInputOpts struct {
Description string `json:"description"`
EngineId string `json:"engineId"`
StreamId string `json:"streamId"`
Title string `json:"title"`
AllowedNetworks []string `json:"allowedNetworks,omitempty"`
ExposedPort *string `json:"exposedPort,omitempty"`
NbInstance *int64 `json:"nbInstance,omitempty"`
Description string `json:"description"`
EngineId string `json:"engineId"`
StreamId string `json:"streamId"`
Title string `json:"title"`
AllowedNetworks []string `json:"allowedNetworks,omitempty"`
ExposedPort *string `json:"exposedPort,omitempty"`
NbInstance *int64 `json:"nbInstance,omitempty"`
Autoscale bool `json:"autoscale"`
MaxScaleInstance *int `json:"maxScaleInstance,omitempty"`
MinScaleInstance *int `json:"minScaleInstance,omitempty"`
}

func (opts *DbaasLogsInputOpts) FromResource(d *schema.ResourceData) *DbaasLogsInputOpts {
Expand All @@ -71,7 +84,7 @@ func (opts *DbaasLogsInputOpts) FromResource(d *schema.ResourceData) *DbaasLogsI
opts.Title = d.Get("title").(string)

networks := d.Get("allowed_networks").([]interface{})
if networks != nil && len(networks) > 0 {
if len(networks) > 0 {
networksString := make([]string, len(networks))
for i, net := range networks {
networksString[i] = net.(string)
Expand All @@ -81,6 +94,12 @@ func (opts *DbaasLogsInputOpts) FromResource(d *schema.ResourceData) *DbaasLogsI

opts.ExposedPort = helpers.GetNilStringPointerFromData(d, "exposed_port")
opts.NbInstance = helpers.GetNilInt64PointerFromData(d, "nb_instance")
if autoscale, ok := d.GetOk("autoscale"); ok {
opts.Autoscale = autoscale.(bool)
}
opts.MaxScaleInstance = helpers.GetNilIntPointerFromDataAndNilIfNotPresent(d, "max_scale_instance")
opts.MinScaleInstance = helpers.GetNilIntPointerFromDataAndNilIfNotPresent(d, "min_scale_instance")

return opts
}

Expand Down
Loading

0 comments on commit 2cdaea4

Please sign in to comment.