Skip to content

Commit

Permalink
Support table monitoring plans (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
idc101 authored Oct 24, 2023
2 parents 699b1e7 + 9cc896e commit 4d6afb6
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 14 deletions.
24 changes: 15 additions & 9 deletions client/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,17 +445,23 @@ type PrincipalId struct {
Type string `json:"adt_type"`
}

type MonitoringPlan struct {
Type string `json:"adt_type"`
Tables []int `json:"tables,omitempty"`
Excluded []int `json:"excluded"`
}

// TableMonitoring ...
type TableMonitoring struct {
ID int `json:"id,omitempty"`
Name string `json:"name"`
Description string `json:"description"`
Tables []int `json:"tables"`
Schedule *Schedule `json:"schedule"`
Cluster int `json:"cluster"`
ClusterPropertySets []int `json:"clusterPropertySets"`
Enabled bool `json:"enabled"`
Principal *int `json:"principal,omitempty"`
ID int `json:"id,omitempty"`
Name string `json:"name"`
Description string `json:"description"`
Plan *MonitoringPlan `json:"plan"`
Schedule *Schedule `json:"schedule"`
Cluster int `json:"cluster"`
ClusterPropertySets []int `json:"clusterPropertySets"`
Enabled bool `json:"enabled"`
Principal *int `json:"principal,omitempty"`
}

type CachingPlan struct {
Expand Down
4 changes: 2 additions & 2 deletions client/resource_table_caching.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func ResourceTableCaching() *schema.Resource {
StateUpgraders: []schema.StateUpgrader{
{
Type: resourceTableCachingV0().CoreConfigSchema().ImpliedType(),
Upgrade: resourceExampleInstanceStateUpgradeV0,
Upgrade: resourceTableCachingUpgradeV0,
Version: 0,
},
},
Expand Down Expand Up @@ -152,7 +152,7 @@ func resourceTableCachingV0() *schema.Resource {
}
}

func resourceExampleInstanceStateUpgradeV0(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
func resourceTableCachingUpgradeV0(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
plan := make(map[string]interface{})
plan["include"] = rawState["spec"]

Expand Down
186 changes: 183 additions & 3 deletions client/resource_table_monitoring.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,96 @@
package anaml

import (
"context"
"errors"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func ResourceTableMonitoring() *schema.Resource {
return &schema.Resource{
Create: resourceTableMonitoringCreate,
Read: resourceTableMonitoringRead,
Update: resourceTableMonitoringUpdate,
Delete: resourceTableMonitoringDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"include": {
Type: schema.TypeList,
Description: "Include specific tables to monitor with this job",
Optional: true,
MaxItems: 1,
Elem: monitoringTables(),
},
"auto": {
Type: schema.TypeList,
Description: "Auto plan, with ability to exclude tables",
Optional: true,
MaxItems: 1,
Elem: excludedTables(),
ExactlyOneOf: []string{"include", "auto"},
},
"principal": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateAnamlIdentifier(),
},
"enabled": {
Type: schema.TypeBool,
Required: true,
},
"daily_schedule": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: dailyScheduleSchema(),
ConflictsWith: []string{"cron_schedule"},
},
"cron_schedule": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: cronScheduleSchema(),
ConflictsWith: []string{"daily_schedule"},
},
"cluster": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateAnamlIdentifier(),
},
"cluster_property_sets": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validateAnamlIdentifier(),
},
},
},
SchemaVersion: 1,
StateUpgraders: []schema.StateUpgrader{
{
Type: ResourceTableMonitoringV0().CoreConfigSchema().ImpliedType(),
Upgrade: resourceTableMonitoringUpgradeV0,
Version: 0,
},
},
}
}

func ResourceTableMonitoringV0() *schema.Resource {
return &schema.Resource{
Create: resourceTableMonitoringCreate,
Read: resourceTableMonitoringRead,
Expand Down Expand Up @@ -75,6 +159,51 @@ func ResourceTableMonitoring() *schema.Resource {
}
}

func excludedTables() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"exclude": {
Type: schema.TypeSet,
Description: "Tables to monitor with this job",
Optional: true,

Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validateAnamlIdentifier(),
},
},
},
}
}

func monitoringTables() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"tables": {
Type: schema.TypeSet,
Description: "Tables to monitor with this job",
Required: true,
MinItems: 1,

Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validateAnamlIdentifier(),
},
},
},
}
}

func resourceTableMonitoringUpgradeV0(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
plan := make(map[string]interface{})
plan["tables"] = rawState["tables"]

rawState["include"] = plan
delete(rawState, "tables")

return rawState, nil
}

func resourceTableMonitoringRead(d *schema.ResourceData, m interface{}) error {
c := m.(*Client)
TableMonitoringID := d.Id()
Expand All @@ -94,7 +223,14 @@ func resourceTableMonitoringRead(d *schema.ResourceData, m interface{}) error {
if err := d.Set("description", TableMonitoring.Description); err != nil {
return err
}
if err := d.Set("tables", identifierList(TableMonitoring.Tables)); err != nil {
if err := d.Set("include", nil); err != nil {
return err
}
if err := d.Set("auto", nil); err != nil {
return err
}
loc, plan := flattenTableMonitoringPlan(TableMonitoring.Plan)
if err := d.Set(loc, plan); err != nil {
return err
}
if err := d.Set("enabled", TableMonitoring.Enabled); err != nil {
Expand Down Expand Up @@ -182,6 +318,11 @@ func composeTableMonitoring(d *schema.ResourceData) (*TableMonitoring, error) {
principal = &principal_
}

plan, err := expandTableMonitoringPlan(d)
if err != nil {
return nil, err
}

if dailySchedule, _ := expandSingleMap(d.Get("daily_schedule")); dailySchedule != nil {
schedule, err := composeDailySchedule(dailySchedule)
if err != nil {
Expand All @@ -190,7 +331,7 @@ func composeTableMonitoring(d *schema.ResourceData) (*TableMonitoring, error) {
return &TableMonitoring{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Tables: expandIdentifierList(d.Get("tables").(*schema.Set).List()),
Plan: plan,
Principal: principal,
Enabled: d.Get("enabled").(bool),
Cluster: cluster,
Expand All @@ -207,7 +348,7 @@ func composeTableMonitoring(d *schema.ResourceData) (*TableMonitoring, error) {
return &TableMonitoring{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Tables: expandIdentifierList(d.Get("tables").(*schema.Set).List()),
Plan: plan,
Principal: principal,
Enabled: d.Get("enabled").(bool),
Cluster: cluster,
Expand Down Expand Up @@ -240,3 +381,42 @@ func resourceTableMonitoringDelete(d *schema.ResourceData, m interface{}) error

return nil
}

func expandTableMonitoringPlan(d *schema.ResourceData) (*MonitoringPlan, error) {
if inclusion, _ := expandSingleMap(d.Get("include")); inclusion != nil {
plan := MonitoringPlan{
Type: "inclusion",
Tables: expandIdentifierList(inclusion["tables"].(*schema.Set).List()),
}
return &plan, nil
}

if auto, _ := expandSingleMap(d.Get("auto")); auto != nil {
plan := MonitoringPlan{
Type: "auto",
Excluded: expandIdentifierList(auto["exclude"].(*schema.Set).List()),
}
return &plan, nil
}

return nil, errors.New("Invalid monitoring plan type")
}

func flattenTableMonitoringPlan(plan *MonitoringPlan) (string, []map[string]interface{}) {
res := make([]map[string]interface{}, 0, 1)
loc := ""

if plan.Type == "inclusion" {
single := make(map[string]interface{})
single["tables"] = identifierList(plan.Tables)
res = append(res, single)
loc = "include"
} else {
single := make(map[string]interface{})
single["exclude"] = identifierList(plan.Excluded)
res = append(res, single)
loc = "auto"
}

return loc, res
}

0 comments on commit 4d6afb6

Please sign in to comment.