Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

feat: Simplify Resources #1385

Merged
merged 18 commits into from
Aug 7, 2022
Merged
34 changes: 4 additions & 30 deletions resources/services/acm/certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package acm

import (
"context"
"encoding/json"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/acm"
Expand Down Expand Up @@ -60,13 +59,13 @@ func AcmCertificates() *schema.Table {
Name: "domain_validation_options",
Description: "Contains information about the initial validation of each domain name that occurs as a result of the RequestCertificate request.",
Type: schema.TypeJSON,
Resolver: resolveACMCertificateJSONField(func(cd *types.CertificateDetail) interface{} { return cd.DomainValidationOptions }),
Resolver: schema.PathResolver("DomainValidationOptions"),
},
{
Name: "extended_key_usages",
Description: "Contains a list of Extended Key Usage X.509 v3 extension objects.",
Type: schema.TypeJSON,
Resolver: resolveACMCertificateJSONField(func(cd *types.CertificateDetail) interface{} { return cd.ExtendedKeyUsages }),
Resolver: schema.PathResolver("ExtendedKeyUsages"),
},
{
Name: "failure_reason",
Expand Down Expand Up @@ -102,7 +101,7 @@ func AcmCertificates() *schema.Table {
Name: "key_usages",
Description: "A list of Key Usage X.509 v3 extension objects. Each object is a string value that identifies the purpose of the public key contained in the certificate.",
Type: schema.TypeStringArray,
Resolver: resolveACMCertificateKeyUsages,
Resolver: schema.PathResolver("KeyUsages.Name"),
},
{
Name: "not_after",
Expand All @@ -129,12 +128,7 @@ func AcmCertificates() *schema.Table {
Name: "renewal_summary_domain_validation_options",
Description: "Contains information about the validation of each domain name in the certificate, as it pertains to ACM's managed renewal.",
Type: schema.TypeJSON,
Resolver: resolveACMCertificateJSONField(func(cd *types.CertificateDetail) interface{} {
if cd.RenewalSummary == nil {
return nil
}
return cd.RenewalSummary.DomainValidationOptions
}),
Resolver: schema.PathResolver("RenewalSummary.DomainValidationOptions"),
},
{
Name: "renewal_summary_status",
Expand Down Expand Up @@ -234,26 +228,6 @@ func fetchAcmCertificates(ctx context.Context, meta schema.ClientMeta, parent *s
return nil
}

func resolveACMCertificateKeyUsages(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
cert := resource.Item.(*types.CertificateDetail)
result := make([]string, 0, len(cert.KeyUsages))
for _, v := range cert.KeyUsages {
result = append(result, string(v.Name))
}
return diag.WrapError(resource.Set(c.Name, result))
}

func resolveACMCertificateJSONField(getter func(*types.CertificateDetail) interface{}) func(context.Context, schema.ClientMeta, *schema.Resource, schema.Column) error {
return func(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
cert := resource.Item.(*types.CertificateDetail)
b, err := json.Marshal(getter(cert))
if err != nil {
return diag.WrapError(err)
}
return diag.WrapError(resource.Set(c.Name, b))
}
}

func resolveACMCertificateTags(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
cert := resource.Item.(*types.CertificateDetail)
cl := meta.(*client.Client)
Expand Down
31 changes: 3 additions & 28 deletions resources/services/applicationautoscaling/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package applicationautoscaling

import (
"context"
"encoding/json"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/applicationautoscaling"
Expand Down Expand Up @@ -82,19 +81,19 @@ func ApplicationautoscalingPolicies() *schema.Table {
Name: "alarms",
Description: "The CloudWatch alarms associated with the scaling policy.",
Type: schema.TypeJSON,
Resolver: resolveApplicationautoscalingPolicyAlarms,
Resolver: schema.PathResolver("Alarms"),
},
{
Name: "step_scaling_policy_configuration",
Description: "A step scaling policy.",
Type: schema.TypeJSON,
Resolver: resolveApplicationautoscalingPolicyStepScalingPolicyConfiguration,
Resolver: schema.PathResolver("StepScalingPolicyConfiguration"),
},
{
Name: "target_tracking_scaling_policy_configuration",
Description: "A target tracking scaling policy.",
Type: schema.TypeJSON,
Resolver: resolveApplicationautoscalingPolicyTargetTrackingScalingPolicyConfiguration,
Resolver: schema.PathResolver("TargetTrackingScalingPolicyConfiguration"),
},
},
}
Expand Down Expand Up @@ -128,27 +127,3 @@ func fetchApplicationautoscalingPolicies(ctx context.Context, meta schema.Client

return nil
}
func resolveApplicationautoscalingPolicyAlarms(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
r := resource.Item.(types.ScalingPolicy)
if r.Alarms == nil {
return nil
}
b, _ := json.Marshal(r.Alarms)
return diag.WrapError(resource.Set(c.Name, b))
}
func resolveApplicationautoscalingPolicyStepScalingPolicyConfiguration(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
r := resource.Item.(types.ScalingPolicy)
if r.StepScalingPolicyConfiguration == nil {
return nil
}
b, _ := json.Marshal(r.StepScalingPolicyConfiguration)
return diag.WrapError(resource.Set(c.Name, b))
}
func resolveApplicationautoscalingPolicyTargetTrackingScalingPolicyConfiguration(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
r := resource.Item.(types.ScalingPolicy)
if r.TargetTrackingScalingPolicyConfiguration == nil {
return nil
}
b, _ := json.Marshal(r.TargetTrackingScalingPolicyConfiguration)
return diag.WrapError(resource.Set(c.Name, b))
}
25 changes: 3 additions & 22 deletions resources/services/autoscaling/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package autoscaling

import (
"context"
"encoding/json"
"errors"
"regexp"

Expand Down Expand Up @@ -217,7 +216,7 @@ func AutoscalingGroups() *schema.Table {
{
Name: "aws_autoscaling_group_instances",
Description: "Describes an EC2 instance.",
Resolver: fetchAutoscalingGroupInstances,
Resolver: schema.PathTableResolver("Instances"),
Columns: []schema.Column{
{
Name: "group_cq_id",
Expand Down Expand Up @@ -294,7 +293,7 @@ func AutoscalingGroups() *schema.Table {
{
Name: "aws_autoscaling_group_tags",
Description: "Describes a tag for an Auto Scaling group.",
Resolver: fetchAutoscalingGroupTags,
Resolver: schema.PathTableResolver("Tags"),
Columns: []schema.Column{
{
Name: "group_cq_id",
Expand Down Expand Up @@ -414,7 +413,7 @@ func AutoscalingGroups() *schema.Table {
Name: "step_adjustments",
Description: "A set of adjustments that enable you to scale based on the size of the alarm breach.",
Type: schema.TypeJSON,
Resolver: resolveAutoscalingGroupScalingPoliciesStepAdjustments,
Resolver: schema.PathResolver("StepAdjustments"),
},
{
Name: "target_tracking_configuration_target_value",
Expand Down Expand Up @@ -684,16 +683,6 @@ func resolveAutoscalingGroupsSuspendedProcesses(ctx context.Context, meta schema

return diag.WrapError(resource.Set(c.Name, j))
}
func fetchAutoscalingGroupInstances(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
p := parent.Item.(autoscalingGroupWrapper)
res <- p.Instances
return nil
}
func fetchAutoscalingGroupTags(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
p := parent.Item.(autoscalingGroupWrapper)
res <- p.Tags
return nil
}
func fetchAutoscalingGroupScalingPolicies(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
p := parent.Item.(autoscalingGroupWrapper)
cl := meta.(*client.Client)
Expand Down Expand Up @@ -727,14 +716,6 @@ func resolveAutoscalingGroupScalingPoliciesAlarms(ctx context.Context, meta sche
}
return diag.WrapError(resource.Set(c.Name, j))
}
func resolveAutoscalingGroupScalingPoliciesStepAdjustments(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
p := resource.Item.(types.ScalingPolicy)
data, err := json.Marshal(p.StepAdjustments)
if err != nil {
return diag.WrapError(err)
}
return diag.WrapError(resource.Set(c.Name, data))
}
func resolveAutoscalingGroupScalingPoliciesTargetTrackingConfigurationCustomizedMetricDimensions(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
p := resource.Item.(types.ScalingPolicy)
if p.TargetTrackingConfiguration == nil || p.TargetTrackingConfiguration.CustomizedMetricSpecification == nil {
Expand Down
63 changes: 5 additions & 58 deletions resources/services/backup/plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package backup

import (
"context"
"encoding/json"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/backup"
"github.com/aws/aws-sdk-go-v2/service/backup/types"
"github.com/cloudquery/cq-provider-aws/client"
"github.com/cloudquery/cq-provider-sdk/provider/diag"
"github.com/cloudquery/cq-provider-sdk/provider/schema"
Expand Down Expand Up @@ -78,7 +76,7 @@ func Plans() *schema.Table {
Name: "advanced_backup_settings",
Description: "Contains a list of backup options for a resource type.",
Type: schema.TypeJSON,
Resolver: resolvePlanAdvancedBackupSettings,
Resolver: schema.PathResolver("AdvancedBackupSettings"),
IgnoreInTests: true,
},
{
Expand All @@ -92,7 +90,7 @@ func Plans() *schema.Table {
{
Name: "aws_backup_plan_rules",
Description: "Specifies a scheduled task used to back up a selection of resources.",
Resolver: fetchPlanRules,
Resolver: schema.PathTableResolver("BackupPlan.Rules"),
IgnoreError: client.IgnoreAccessDeniedServiceDisabled,
Columns: []schema.Column{
{
Expand Down Expand Up @@ -121,7 +119,7 @@ func Plans() *schema.Table {
Name: "copy_actions",
Description: "The details of the copy operation.",
Type: schema.TypeJSON,
Resolver: resolveRuleCopyActions,
Resolver: schema.PathResolver("CopyActions"),
IgnoreInTests: true,
},
{
Expand Down Expand Up @@ -211,13 +209,13 @@ func Plans() *schema.Table {
Name: "conditions",
Description: "A list of conditions that you define to assign resources to your backup plans using tags.",
Type: schema.TypeJSON,
Resolver: resolveSelectionConditions,
Resolver: schema.PathResolver("BackupSelection.Conditions"),
},
{
Name: "list_of_tags",
Description: "A list of conditions that you define to assign resources to your backup plans using tags.",
Type: schema.TypeJSON,
Resolver: resolveSelectionListOfTags,
Resolver: schema.PathResolver("BackupSelection.ListOfTags"),
},
{
Name: "not_resources",
Expand Down Expand Up @@ -273,15 +271,6 @@ func fetchBackupPlans(ctx context.Context, meta schema.ClientMeta, parent *schem
return nil
}

func resolvePlanAdvancedBackupSettings(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
plan := resource.Item.(backup.GetBackupPlanOutput)
b, err := json.Marshal(plan.AdvancedBackupSettings)
if err != nil {
return diag.WrapError(err)
}
return diag.WrapError(resource.Set(c.Name, b))
}

func resolvePlanTags(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
plan := resource.Item.(backup.GetBackupPlanOutput)
cl := meta.(*client.Client)
Expand Down Expand Up @@ -342,45 +331,3 @@ func fetchBackupSelections(ctx context.Context, meta schema.ClientMeta, parent *
}
return nil
}

func fetchPlanRules(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
plan := parent.Item.(backup.GetBackupPlanOutput)
if plan.BackupPlan == nil {
return nil
}
res <- plan.BackupPlan.Rules
return nil
}

func resolveRuleCopyActions(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
rule := resource.Item.(types.BackupRule)
b, err := json.Marshal(rule.CopyActions)
if err != nil {
return diag.WrapError(err)
}
return diag.WrapError(resource.Set(c.Name, b))
}

func resolveSelectionConditions(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
s := resource.Item.(backup.GetBackupSelectionOutput)
if s.BackupSelection == nil || s.BackupSelection.Conditions == nil {
return nil
}
b, err := json.Marshal(s.BackupSelection.Conditions)
if err != nil {
return diag.WrapError(err)
}
return diag.WrapError(resource.Set(c.Name, b))
}

func resolveSelectionListOfTags(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
s := resource.Item.(backup.GetBackupSelectionOutput)
if s.BackupSelection == nil || len(s.BackupSelection.ListOfTags) == 0 {
return nil
}
b, err := json.Marshal(s.BackupSelection.ListOfTags)
if err != nil {
return diag.WrapError(err)
}
return diag.WrapError(resource.Set(c.Name, b))
}
12 changes: 1 addition & 11 deletions resources/services/backup/vaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package backup

import (
"context"
"encoding/json"
"errors"
"strings"

Expand Down Expand Up @@ -160,7 +159,7 @@ func Vaults() *schema.Table {
Name: "created_by",
Description: "Contains identifying information about the creation of a recovery point.",
Type: schema.TypeJSON,
Resolver: resolveVaultRecoveryPointCreatedBy,
Resolver: schema.PathResolver("CreatedBy"),
},
{
Name: "creation_date",
Expand Down Expand Up @@ -356,15 +355,6 @@ func fetchVaultRecoveryPoints(ctx context.Context, meta schema.ClientMeta, paren
return nil
}

func resolveVaultRecoveryPointCreatedBy(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
rp := resource.Item.(types.RecoveryPointByBackupVault)
b, err := json.Marshal(rp.CreatedBy)
if err != nil {
return diag.WrapError(err)
}
return diag.WrapError(resource.Set(c.Name, b))
}

func resolveRecoveryPointTags(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
rp := resource.Item.(types.RecoveryPointByBackupVault)
if rp.ResourceArn == nil || rp.RecoveryPointArn == nil {
Expand Down
Loading