Skip to content

Commit 7c86d83

Browse files
Michael SzilagyiMaxrovr
authored andcommitted
Added - Support for Autoscaling to support Custom Metrics feature
1 parent 8f7b4c0 commit 7c86d83

8 files changed

+439
-57
lines changed

examples/autoscaling/compute.tf

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ resource "oci_core_instance" "TFInstance" {
1919
source_id = var.instance_image_ocid[var.region]
2020
}
2121

22+
instance_options {
23+
are_legacy_imds_endpoints_disabled = true
24+
}
25+
2226
timeouts {
2327
create = "60m"
2428
}
@@ -59,7 +63,7 @@ resource "oci_core_instance_configuration" "TFInstanceConfiguration" {
5963
resource "oci_core_instance_pool" "TFInstancePool" {
6064
compartment_id = var.compartment_ocid
6165
instance_configuration_id = oci_core_instance_configuration.TFInstanceConfiguration.id
62-
size = 2
66+
size = 1
6367
state = "RUNNING"
6468
display_name = "TFInstancePool"
6569

@@ -72,7 +76,7 @@ resource "oci_core_instance_pool" "TFInstancePool" {
7276
resource "oci_core_instance_pool" "TFInstancePoolForScheduledPolicy" {
7377
compartment_id = var.compartment_ocid
7478
instance_configuration_id = oci_core_instance_configuration.TFInstanceConfiguration.id
75-
size = 2
79+
size = 1
7680
state = "RUNNING"
7781
display_name = "TFInstancePoolForScheduledPolicy"
7882

@@ -95,6 +99,19 @@ resource "oci_core_instance_pool" "TFInstancePoolForScheduledPolicyResourceActio
9599
}
96100
}
97101

102+
resource "oci_core_instance_pool" "TFInstancePoolForCustomMetrics" {
103+
compartment_id = var.compartment_ocid
104+
instance_configuration_id = oci_core_instance_configuration.TFInstanceConfiguration.id
105+
size = 1
106+
state = "RUNNING"
107+
display_name = "TFInstancePoolForCustomMetrics"
108+
109+
placement_configurations {
110+
availability_domain = data.oci_identity_availability_domain.AD.name
111+
primary_subnet_id = oci_core_subnet.ExampleSubnet.id
112+
}
113+
}
114+
98115
resource "oci_autoscaling_auto_scaling_configuration" "TFAutoScalingConfiguration" {
99116
compartment_id = var.compartment_ocid
100117
cool_down_in_seconds = "300"
@@ -103,9 +120,9 @@ resource "oci_autoscaling_auto_scaling_configuration" "TFAutoScalingConfiguratio
103120

104121
policies {
105122
capacity {
106-
initial = "2"
107-
max = "4"
108-
min = "2"
123+
initial = "1"
124+
max = "2"
125+
min = "1"
109126
}
110127

111128
display_name = "TFPolicy"
@@ -209,3 +226,62 @@ resource "oci_autoscaling_auto_scaling_configuration" "TFAutoScalingConfiguratio
209226
type = "instancePool"
210227
}
211228
}
229+
230+
resource "oci_autoscaling_auto_scaling_configuration" "TFAutoScalingConfigurationCustomMetrics" {
231+
compartment_id = var.compartment_ocid
232+
cool_down_in_seconds = "300"
233+
display_name = "TFAutoScalingConfigurationCustomMetrics"
234+
is_enabled = "true"
235+
236+
policies {
237+
capacity {
238+
initial = "1"
239+
max = "2"
240+
min = "1"
241+
}
242+
243+
display_name = "TFPolicyCustomMetrics"
244+
policy_type = "threshold"
245+
246+
rules {
247+
action {
248+
type = "CHANGE_COUNT_BY"
249+
value = "1"
250+
}
251+
252+
display_name = "TFScaleOutRuleCustomMetrics"
253+
254+
metric {
255+
metric_source = "CUSTOM_QUERY"
256+
query = "CpuUtilization[1m]{instancePoolId=oci_core_instance_pool.rana_instance_pool.id}.groupBy(instancePoolId).mean() > 0"
257+
namespace = "oci_computeagent"
258+
resource_group = "resource_group"
259+
metric_compartment_id = var.compartment_ocid
260+
pending_duration = "PT5M"
261+
}
262+
}
263+
264+
rules {
265+
action {
266+
type = "CHANGE_COUNT_BY"
267+
value = "-1"
268+
}
269+
270+
display_name = "TFScaleInRuleCustomMetrics"
271+
272+
metric {
273+
metric_source = "CUSTOM_QUERY"
274+
query = "CpuUtilization[1m]{instancePoolId=oci_core_instance_pool.rana_instance_pool.id}.groupBy(instancePoolId).mean() > 0"
275+
namespace = "oci_computeagent"
276+
resource_group = "resource_group"
277+
metric_compartment_id = var.compartment_ocid
278+
pending_duration = "PT5M"
279+
}
280+
}
281+
}
282+
283+
auto_scaling_resources {
284+
id = oci_core_instance_pool.TFInstancePoolForCustomMetrics.id
285+
type = "instancePool"
286+
}
287+
}

examples/autoscaling/datasources.tf

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ data "oci_core_instance_pools" "TFInstancePoolDatasources" {
3838
data "oci_core_instance_pool_instances" "TFInstancePoolInstanceDatasources" {
3939
compartment_id = var.compartment_ocid
4040
instance_pool_id = oci_core_instance_pool.TFInstancePool.id
41-
display_name = "TFInstancePool"
4241
}
4342

4443
data "oci_autoscaling_auto_scaling_configuration" "TFAutoScalingConfigurationDatasource" {
@@ -53,6 +52,10 @@ data "oci_autoscaling_auto_scaling_configuration" "TFAutoScalingConfigurationSch
5352
auto_scaling_configuration_id = oci_autoscaling_auto_scaling_configuration.TFAutoScalingConfigurationScheduledPolicyResourceAction.id
5453
}
5554

55+
data "oci_autoscaling_auto_scaling_configuration" "TFAutoScalingConfigurationCustomMetricsDatasource" {
56+
auto_scaling_configuration_id = oci_autoscaling_auto_scaling_configuration.TFAutoScalingConfigurationCustomMetrics.id
57+
}
58+
5659
data "oci_autoscaling_auto_scaling_configurations" "TFAutoScalingConfigurationDatasources" {
5760
compartment_id = var.compartment_ocid
5861
display_name = "TFAutoScalingConfiguration"
@@ -65,7 +68,7 @@ data "oci_autoscaling_auto_scaling_configurations" "TFAutoScalingConfigurationDa
6568

6669
// Usage of singular instance datasources to show the public_ips, private_ips, and hostname_labels for the instances in the pool
6770
data "oci_core_instance" "TFInstancePoolInstanceSingularDatasources" {
68-
count = 2
71+
count = 1
6972
instance_id = data.oci_core_instance_pool_instances.TFInstancePoolInstanceDatasources.instances[count.index]["id"]
7073
}
7174

examples/autoscaling/provider.tf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@ provider "oci" {
77
fingerprint = var.fingerprint
88
private_key_path = var.private_key_path
99
region = var.region
10-
}
11-
10+
}

internal/integrationtest/autoscaling_auto_scaling_configuration_test.go

Lines changed: 97 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ var (
5353
"freeform_tags": acctest.Representation{RepType: acctest.Optional, Create: map[string]string{"Department": "Finance"}, Update: map[string]string{"Department": "Accounting"}},
5454
"is_enabled": acctest.Representation{RepType: acctest.Optional, Create: `false`, Update: `true`},
5555
}
56+
57+
AutoScalingAutoScalingConfigurationResourceDependenciesCustomQuery = map[string]interface{}{
58+
"auto_scaling_resources": acctest.RepresentationGroup{RepType: acctest.Required, Group: AutoScalingautoScalingConfigurationAutoScalingResourcesRepresentation},
59+
"compartment_id": acctest.Representation{RepType: acctest.Required, Create: `${var.compartment_id}`},
60+
"policies": acctest.RepresentationGroup{RepType: acctest.Required, Group: AutoScalingautoScalingConfigurationPoliciesRepresentationCustomQuery},
61+
"cool_down_in_seconds": acctest.Representation{RepType: acctest.Optional, Create: `300`, Update: `400`},
62+
"defined_tags": acctest.Representation{RepType: acctest.Optional, Create: `${map("${oci_identity_tag_namespace.tag-namespace1.name}.${oci_identity_tag.tag1.name}", "value")}`, Update: `${map("${oci_identity_tag_namespace.tag-namespace1.name}.${oci_identity_tag.tag1.name}", "updatedValue")}`},
63+
"display_name": acctest.Representation{RepType: acctest.Optional, Create: `example_threshold_autoscaling_configuration`, Update: `displayName2`},
64+
"freeform_tags": acctest.Representation{RepType: acctest.Optional, Create: map[string]string{"Department": "Finance"}, Update: map[string]string{"Department": "Accounting"}},
65+
"is_enabled": acctest.Representation{RepType: acctest.Optional, Create: `false`, Update: `true`},
66+
}
5667
AutoScalingautoScalingConfigurationAutoScalingResourcesRepresentation = map[string]interface{}{
5768
"id": acctest.Representation{RepType: acctest.Required, Create: `${oci_core_instance_pool.test_instance_pool.id}`},
5869
"type": acctest.Representation{RepType: acctest.Required, Create: `instancePool`},
@@ -63,6 +74,12 @@ var (
6374
"rules": []acctest.RepresentationGroup{{RepType: acctest.Required, Group: AutoScalingautoScalingConfigurationPoliciesScaleOutRuleRepresentation}, {RepType: acctest.Required, Group: AutoScalingautoScalingConfigurationPoliciesScaleInRuleRepresentation}},
6475
"display_name": acctest.Representation{RepType: acctest.Optional, Create: `example_autoscaling_configuration`, Update: `displayName2`},
6576
}
77+
AutoScalingautoScalingConfigurationPoliciesRepresentationCustomQuery = map[string]interface{}{
78+
"capacity": acctest.RepresentationGroup{RepType: acctest.Required, Group: AutoScalingautoScalingConfigurationPoliciesCapacityRepresentation},
79+
"policy_type": acctest.Representation{RepType: acctest.Required, Create: `threshold`, Update: `threshold`},
80+
"rules": []acctest.RepresentationGroup{{RepType: acctest.Required, Group: AutoScalingautoScalingConfigurationPoliciesScaleOutRuleRepresentationCustomQuery}, {RepType: acctest.Required, Group: AutoScalingautoScalingConfigurationPoliciesScaleInRuleRepresentationCustomQuery}},
81+
"display_name": acctest.Representation{RepType: acctest.Optional, Create: `example_autoscaling_configuration`, Update: `displayName2`},
82+
}
6683
AutoScalingautoScalingConfigurationPoliciesCapacityRepresentation = map[string]interface{}{
6784
"initial": acctest.Representation{RepType: acctest.Required, Create: `2`, Update: `4`},
6885
"max": acctest.Representation{RepType: acctest.Required, Create: `3`, Update: `5`},
@@ -73,13 +90,25 @@ var (
7390
"display_name": acctest.Representation{RepType: acctest.Required, Create: `scale out rule`, Update: `scale out rule - updated`},
7491
"metric": acctest.RepresentationGroup{RepType: acctest.Required, Group: AutoScalingautoScalingConfigurationPoliciesScaleOutRuleMetricRepresentation},
7592
}
93+
AutoScalingautoScalingConfigurationPoliciesScaleOutRuleRepresentationCustomQuery = map[string]interface{}{
94+
"action": acctest.RepresentationGroup{RepType: acctest.Required, Group: AutoScalingautoScalingConfigurationPoliciesScaleOutRuleActionRepresentation},
95+
"display_name": acctest.Representation{RepType: acctest.Required, Create: `scale out rule`, Update: `scale out rule - updated`},
96+
"metric": acctest.RepresentationGroup{RepType: acctest.Required, Group: AutoScalingautoScalingConfigurationPoliciesRuleRepresentationCustomQuery},
97+
}
98+
AutoScalingautoScalingConfigurationPoliciesScaleInRuleRepresentationCustomQuery = map[string]interface{}{
99+
"action": acctest.RepresentationGroup{RepType: acctest.Required, Group: AutoScalingautoScalingConfigurationPoliciesScaleInRuleActionRepresentation},
100+
"display_name": acctest.Representation{RepType: acctest.Required, Create: `scale in rule`, Update: `scale in rule - updated`},
101+
"metric": acctest.RepresentationGroup{RepType: acctest.Required, Group: AutoScalingautoScalingConfigurationPoliciesRuleRepresentationCustomQuery},
102+
}
76103
AutoScalingautoScalingConfigurationPoliciesScaleOutRuleActionRepresentation = map[string]interface{}{
77104
"type": acctest.Representation{RepType: acctest.Required, Create: `CHANGE_COUNT_BY`, Update: `CHANGE_COUNT_BY`},
78105
"value": acctest.Representation{RepType: acctest.Required, Create: `1`, Update: `2`},
79106
}
80107
AutoScalingautoScalingConfigurationPoliciesScaleOutRuleMetricRepresentation = map[string]interface{}{
81-
"metric_type": acctest.Representation{RepType: acctest.Required, Create: `CPU_UTILIZATION`, Update: `CPU_UTILIZATION`},
82-
"threshold": acctest.RepresentationGroup{RepType: acctest.Required, Group: AutoScalingautoScalingConfigurationPoliciesScaleOutRuleMetricThresholdRepresentation},
108+
"metric_source": acctest.Representation{RepType: acctest.Required, Create: `COMPUTE_AGENT`},
109+
"pending_duration": acctest.Representation{RepType: acctest.Required, Create: `PT3M`},
110+
"metric_type": acctest.Representation{RepType: acctest.Required, Create: `CPU_UTILIZATION`, Update: `CPU_UTILIZATION`},
111+
"threshold": acctest.RepresentationGroup{RepType: acctest.Required, Group: AutoScalingautoScalingConfigurationPoliciesScaleOutRuleMetricThresholdRepresentation},
83112
}
84113
AutoScalingautoScalingConfigurationPoliciesScaleOutRuleMetricThresholdRepresentation = map[string]interface{}{
85114
"operator": acctest.Representation{RepType: acctest.Required, Create: `GT`, Update: `GT`},
@@ -90,13 +119,24 @@ var (
90119
"metric": acctest.RepresentationGroup{RepType: acctest.Required, Group: AutoScalingautoScalingConfigurationPoliciesScaleInRuleMetricRepresentation},
91120
"display_name": acctest.Representation{RepType: acctest.Required, Create: `scale in rule`, Update: `scale in rule - updated`},
92121
}
122+
AutoScalingautoScalingConfigurationPoliciesRuleRepresentationCustomQuery = map[string]interface{}{
123+
"metric_compartment_id": acctest.Representation{RepType: acctest.Required, Create: `${var.compartment_id}`},
124+
"metric_source": acctest.Representation{RepType: acctest.Required, Create: `CUSTOM_QUERY`},
125+
"namespace": acctest.Representation{RepType: acctest.Required, Create: `oci_computeagent`},
126+
"pending_duration": acctest.Representation{RepType: acctest.Required, Create: `PT3M`},
127+
"query": acctest.Representation{RepType: acctest.Required, Create: `CM-removeLbBackendsWf.detachInstancePoolInstanceWorkflow.removeLbBackendFailureCount[30m]{availabilityDomain=iad-ad-2}.groupBy(availabilityDomain).sum() > 0`},
128+
"resource_group": acctest.Representation{RepType: acctest.Required, Create: `resourceGroup`},
129+
}
130+
93131
AutoScalingautoScalingConfigurationPoliciesScaleInRuleActionRepresentation = map[string]interface{}{
94132
"type": acctest.Representation{RepType: acctest.Required, Create: `CHANGE_COUNT_BY`, Update: `CHANGE_COUNT_BY`},
95133
"value": acctest.Representation{RepType: acctest.Required, Create: `-1`, Update: `-3`},
96134
}
97135
AutoScalingautoScalingConfigurationPoliciesScaleInRuleMetricRepresentation = map[string]interface{}{
98-
"metric_type": acctest.Representation{RepType: acctest.Required, Create: `CPU_UTILIZATION`, Update: `CPU_UTILIZATION`},
99-
"threshold": acctest.RepresentationGroup{RepType: acctest.Required, Group: AutoScalingautoScalingConfigurationPoliciesScaleInRuleMetricThresholdRepresentation},
136+
"metric_source": acctest.Representation{RepType: acctest.Required, Create: `COMPUTE_AGENT`},
137+
"metric_type": acctest.Representation{RepType: acctest.Required, Create: `CPU_UTILIZATION`, Update: `CPU_UTILIZATION`},
138+
"threshold": acctest.RepresentationGroup{RepType: acctest.Required, Group: AutoScalingautoScalingConfigurationPoliciesScaleInRuleMetricThresholdRepresentation},
139+
"pending_duration": acctest.Representation{RepType: acctest.Required, Create: `PT3M`},
100140
}
101141
AutoScalingautoScalingConfigurationPoliciesScaleInRuleMetricThresholdRepresentation = map[string]interface{}{
102142
"operator": acctest.Representation{RepType: acctest.Required, Create: `LT`, Update: `LT`},
@@ -176,6 +216,59 @@ func TestAutoScalingAutoScalingConfigurationResource_basic(t *testing.T) {
176216
),
177217
},
178218

219+
// delete before next Create
220+
{
221+
Config: config + compartmentIdVariableStr + AutoScalingAutoScalingConfigurationResourceDependencies,
222+
},
223+
{
224+
Config: config + compartmentIdVariableStr + AutoScalingAutoScalingConfigurationResourceDependencies +
225+
acctest.GenerateResourceFromRepresentationMap("oci_autoscaling_auto_scaling_configuration", "test_auto_scaling_configuration", acctest.Required, acctest.Create, AutoScalingAutoScalingConfigurationResourceDependenciesCustomQuery),
226+
Check: acctest.ComposeAggregateTestCheckFuncWrapper(
227+
resource.TestCheckResourceAttr(resourceName, "auto_scaling_resources.#", "1"),
228+
resource.TestCheckResourceAttrSet(resourceName, "auto_scaling_resources.0.id"),
229+
resource.TestCheckResourceAttr(resourceName, "auto_scaling_resources.0.type", "instancePool"),
230+
resource.TestCheckResourceAttr(resourceName, "compartment_id", compartmentId),
231+
resource.TestCheckResourceAttr(resourceName, "policies.#", "1"),
232+
resource.TestCheckResourceAttr(resourceName, "policies.0.capacity.#", "1"),
233+
resource.TestCheckResourceAttr(resourceName, "policies.0.capacity.0.initial", "2"),
234+
resource.TestCheckResourceAttr(resourceName, "policies.0.capacity.0.max", "3"),
235+
resource.TestCheckResourceAttr(resourceName, "policies.0.capacity.0.min", "2"),
236+
resource.TestCheckResourceAttr(resourceName, "policies.0.policy_type", "threshold"),
237+
resource.TestCheckResourceAttr(resourceName, "policies.0.rules.#", "2"),
238+
acctest.CheckResourceSetContainsElementWithProperties(resourceName, "policies.0.rules", map[string]string{
239+
"action.#": "1",
240+
"action.0.type": "CHANGE_COUNT_BY",
241+
"action.0.value": "1",
242+
"metric.#": "1",
243+
"metric.0.metric_compartment_id": compartmentId,
244+
"metric.0.metric_source": "CUSTOM_QUERY",
245+
"metric.0.namespace": "oci_computeagent",
246+
"metric.0.pending_duration": "PT3M",
247+
"metric.0.query": "CM-removeLbBackendsWf.detachInstancePoolInstanceWorkflow.removeLbBackendFailureCount[30m]{availabilityDomain=iad-ad-2}.groupBy(availabilityDomain).sum() > 0",
248+
"metric.0.resource_group": "resourceGroup",
249+
},
250+
[]string{}),
251+
acctest.CheckResourceSetContainsElementWithProperties(resourceName, "policies.0.rules", map[string]string{
252+
"action.#": "1",
253+
"action.0.type": "CHANGE_COUNT_BY",
254+
"action.0.value": "1",
255+
"metric.#": "1",
256+
"metric.0.metric_compartment_id": compartmentId,
257+
"metric.0.metric_source": "CUSTOM_QUERY",
258+
"metric.0.namespace": "oci_computeagent",
259+
"metric.0.pending_duration": "PT3M",
260+
"metric.0.query": "CM-removeLbBackendsWf.detachInstancePoolInstanceWorkflow.removeLbBackendFailureCount[30m]{availabilityDomain=iad-ad-2}.groupBy(availabilityDomain).sum() > 0",
261+
"metric.0.resource_group": "resourceGroup",
262+
},
263+
[]string{}),
264+
265+
func(s *terraform.State) (err error) {
266+
resId, err = acctest.FromInstanceState(s, resourceName, "id")
267+
return err
268+
},
269+
),
270+
},
271+
179272
// delete before next Create
180273
{
181274
Config: config + compartmentIdVariableStr + AutoScalingAutoScalingConfigurationResourceDependencies,

0 commit comments

Comments
 (0)