Skip to content

Commit

Permalink
Add RDS DB Subnet Group
Browse files Browse the repository at this point in the history
  • Loading branch information
alpineriveredge committed Aug 3, 2022
1 parent 088978e commit bb48fc2
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 2 deletions.
25 changes: 25 additions & 0 deletions doc/_resource_types/rds_db_subnet_group.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
### exist

```ruby
describe rds_db_subnet_group('my-rds-db-subnet-group') do
it { should exist }
end
```

### belong_to_subnet

```ruby
describe rds_db_subnet_group('my-rds-db-subnet-group') do
it { should belong_to_subnet('subnet-1234a567') }
it { should belong_to_subnet('db-subnet-a') }
end
```

### belong_to_vpc

```ruby
describe rds_db_subnet_group('my-rds-db-subnet-group') do
it { should belong_to_vpc('vpc-ab123cde') }
it { should belong_to_vpc('my-vpc') }
end
```
34 changes: 34 additions & 0 deletions doc/resource_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
| [rds_db_cluster](#rds_db_cluster)
| [rds_db_cluster_parameter_group](#rds_db_cluster_parameter_group)
| [rds_db_parameter_group](#rds_db_parameter_group)
| [rds_db_subnet_group](#rds_db_subnet_group)
| [rds_global_cluster](#rds_global_cluster)
| [rds_proxy](#rds_proxy)
| [redshift](#redshift)
Expand Down Expand Up @@ -2973,6 +2974,39 @@ end
```


## <a name="rds_db_subnet_group">rds_db_subnet_group</a>

RdsDbSubnetGroup resource type.

### exist

```ruby
describe rds_db_subnet_group('my-rds-db-subnet-group') do
it { should exist }
end
```


### belong_to_subnet

```ruby
describe rds_db_subnet_group('my-rds-db-subnet-group') do
it { should belong_to_subnet('subnet-1234a567') }
it { should belong_to_subnet('db-subnet-a') }
end
```


### belong_to_vpc

```ruby
describe rds_db_subnet_group('my-rds-db-subnet-group') do
it { should belong_to_vpc('vpc-ab123cde') }
it { should belong_to_vpc('my-vpc') }
end
```

### its(:vpc_id), its(:db_subnet_group_name), its(:db_subnet_group_description), its(:vpc_id), its(:subnet_group_status), its(:db_subnet_group_arn), its(:supported_network_types)
## <a name="rds_global_cluster">rds_global_cluster</a>

RdsGlobalCluster resource type.
Expand Down
19 changes: 19 additions & 0 deletions lib/awspec/generator/doc/type/rds_db_subnet_group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Awspec::Generator
module Doc
module Type
class RdsDbSubnetGroup < Base
def initialize
super
@type_name = 'RdsDbSubnetGroup'
@type = Awspec::Type::RdsDbSubnetGroup.new('my-rds-db-subnet-group')
@ret = @type.resource_via_client
@matchers = %w[belong_to_vpc belong_to_subnet]
@ignore_matchers = []
@describes = %w[vpc_id]
end
end
end
end
end
7 changes: 7 additions & 0 deletions lib/awspec/helper/finder/rds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ def find_global_cluster(global_cluster_identifier)
})
res.global_clusters.single_resource(global_cluster_identifier)
end

def find_db_subnet_group(db_subnet_group_name)
res = rds_client.describe_db_subnet_groups({
db_subnet_group_name: db_subnet_group_name
})
res.db_subnet_groups.single_resource(db_subnet_group_name)
end
end
end
end
3 changes: 2 additions & 1 deletion lib/awspec/helper/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ module Type
efs eks eks_nodegroup elasticache elasticache_cache_parameter_group elasticsearch elb emr firehose iam_group
iam_policy iam_role iam_user kinesis kms lambda launch_configuration launch_template mq nat_gateway
network_acl network_interface nlb nlb_listener nlb_target_group
rds rds_proxy rds_db_cluster_parameter_group rds_db_parameter_group rds_db_cluster rds_global_cluster
rds rds_proxy rds_db_cluster_parameter_group rds_db_parameter_group rds_db_subnet_group
rds_db_cluster rds_global_cluster
route53_hosted_zone route_table s3_bucket security_group ses_identity subnet vpc cloudfront_distribution
elastictranscoder_pipeline waf_web_acl wafregional_web_acl customer_gateway vpn_gateway vpn_connection
internet_gateway acm cloudwatch_logs dynamodb_table eip sqs ssm_parameter cloudformation_stack
Expand Down
21 changes: 21 additions & 0 deletions lib/awspec/matcher/belong_to_subnet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,27 @@
return ret[:subnet_availability_zone][:name] == type.availability_zone if ret
end

# RDS DB Subnet Group
if type.instance_of?(Awspec::Type::RdsDbSubnetGroup)
subnets = type.resource_via_client[:subnets]
ret = subnets.find do |s|
s[:subnet_identifier] == subnet_id
end

return true if ret

res = type.ec2_client.describe_subnets({
filters: [{ name: 'tag:Name', values: [subnet_id] }]
})
return false unless res

ret = subnets.find do |s|
s[:subnet_identifier] == res[:subnets][0][:subnet_id]
end

return ret ? true : false
end

# RDS Proxy
if type.instance_of?(Awspec::Type::RdsProxy)
subnet_ids = type.resource_via_client[:vpc_subnet_ids]
Expand Down
1 change: 0 additions & 1 deletion lib/awspec/stub/rds_db_cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@
storage_type: nil,
iops: nil,
publicly_accessible: nil,
auto_minor_version_upgrade: false,
monitoring_interval: nil,
monitoring_role_arn: nil,
performance_insights_enabled: nil,
Expand Down
81 changes: 81 additions & 0 deletions lib/awspec/stub/rds_db_subnet_group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# frozen_string_literal: true

Aws.config[:rds] = {
stub_responses: {
describe_db_subnet_groups: {
db_subnet_groups: [
{
db_subnet_group_name: 'my-rds-db-subnet-group',
db_subnet_group_description: 'Created from the RDS Management Console',
vpc_id: 'vpc-ab123cde',
subnet_group_status: 'Complete',
subnets: [
{
subnet_identifier: 'subnet-1234a567',
subnet_availability_zone: {
name: 'ap-northeast-1a'
},
subnet_outpost: {
arn: nil
},
subnet_status: 'Active'
},
{
subnet_identifier: 'subnet-1234b567',
subnet_availability_zone: {
name: 'ap-northeast-1c'
},
subnet_outpost: {
arn: nil
},
subnet_status: 'Active'
},
{
subnet_identifier: 'subnet-1234c567',
subnet_availability_zone: {
name: 'ap-northeast-1d'
},
subnet_outpost: {
arn: nil
},
subnet_status: 'Active'
}
],
db_subnet_group_arn: 'arn:aws:rds:ap-northeast-1:123456789012:subgrp:my-rds-db-subnet-group'
}
],
marker: nil
}
}
}

Aws.config[:ec2] = {
stub_responses: {
describe_vpcs: {
vpcs: [
{
vpc_id: 'vpc-ab123cde',
tags: [
{
key: 'Name',
value: 'my-vpc'
}
]
}
]
},
describe_subnets: {
subnets: [
{
subnet_id: 'subnet-1234a567',
tags: [
{
key: 'Name',
value: 'db-subnet-a'
}
]
}
]
}
}
}
19 changes: 19 additions & 0 deletions lib/awspec/type/rds_db_subnet_group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Awspec::Type
class RdsDbSubnetGroup < ResourceBase
aws_resource Aws::RDS::Types::DBSubnetGroup

def resource_via_client
@resource_via_client ||= find_db_subnet_group(@display_name)
end

def id
@id ||= resource_via_client.db_subnet_group_name if resource_via_client
end

def vpc_id
resource_via_client.vpc_id
end
end
end
12 changes: 12 additions & 0 deletions spec/type/rds_db_subnet_group_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

require 'spec_helper'
Awspec::Stub.load 'rds_db_subnet_group'

describe rds_db_subnet_group('my-rds-db-subnet-group') do
it { should exist }
it { should belong_to_vpc('vpc-ab123cde') }
it { should belong_to_vpc('my-vpc') }
it { should belong_to_subnet('subnet-1234a567') }
it { should belong_to_subnet('db-subnet-a') }
end

0 comments on commit bb48fc2

Please sign in to comment.