Skip to content

Commit 76f65a2

Browse files
authored
Merge pull request #108 from grahamc/defntype
Fixup config / creation issues to work a bit more reliably
2 parents c4713bb + 898714c commit 76f65a2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+379
-282
lines changed

nixops_aws/backends/ec2.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ def host_key_type(self):
103103
class EC2State(MachineState[EC2Definition], EC2CommonState):
104104
"""State of an EC2 machine."""
105105

106+
definition_type = EC2Definition
107+
106108
@classmethod
107109
def get_type(cls):
108110
return "ec2"

nixops_aws/resources/aws_vpn_connection.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ def show_type(self):
3232
class AWSVPNConnectionState(nixops.resources.DiffEngineResourceState, EC2CommonState):
3333
"""State of a AWS VPN gateway."""
3434

35+
definition_type = AWSVPNConnectionDefinition
36+
3537
state = nixops.util.attr_property(
3638
"state", nixops.resources.DiffEngineResourceState.MISSING, int
3739
)
@@ -82,7 +84,7 @@ def create_after(self, resources, defn):
8284
}
8385

8486
def realize_create_vpn_conn(self, allow_recreate):
85-
config = self.get_defn()
87+
config: AWSVPNConnectionDefinition = self.get_defn()
8688

8789
if self.state == self.UP:
8890
if not allow_recreate:
@@ -95,8 +97,8 @@ def realize_create_vpn_conn(self, allow_recreate):
9597
self.warn("vpn connection definition changed, recreating ...")
9698
self._destroy()
9799

98-
self._state["region"] = config["region"]
99-
customer_gtw_id = config["customerGatewayId"]
100+
self._state["region"] = config.config.region
101+
customer_gtw_id = config.config.customerGatewayId
100102
if customer_gtw_id.startswith("res-"):
101103
res_vpc_customer_gw = self.depl.get_typed_resource(
102104
customer_gtw_id[4:].split(".")[0],
@@ -105,7 +107,7 @@ def realize_create_vpn_conn(self, allow_recreate):
105107
)
106108
customer_gtw_id = res_vpc_customer_gw._state["customerGatewayId"]
107109

108-
vpn_gateway_id = config["vpnGatewayId"]
110+
vpn_gateway_id = config.config.vpnGatewayId
109111
if vpn_gateway_id.startswith("res-"):
110112
res_vpn_gateway = self.depl.get_typed_resource(
111113
vpn_gateway_id[4:].split(".")[0], "aws-vpn-gateway", AWSVPNGatewayState
@@ -121,7 +123,7 @@ def realize_create_vpn_conn(self, allow_recreate):
121123
CustomerGatewayId=customer_gtw_id,
122124
VpnGatewayId=vpn_gateway_id,
123125
Type="ipsec.1",
124-
Options={"StaticRoutesOnly": config["staticRoutesOnly"]},
126+
Options={"StaticRoutesOnly": config.config.staticRoutesOnly},
125127
)
126128

127129
vpn_conn_id = vpn_connection["VpnConnection"]["VpnConnectionId"]
@@ -130,11 +132,11 @@ def realize_create_vpn_conn(self, allow_recreate):
130132
self._state["vpnConnectionId"] = vpn_conn_id
131133
self._state["vpnGatewayId"] = vpn_gateway_id
132134
self._state["customerGatewayId"] = customer_gtw_id
133-
self._state["staticRoutesOnly"] = config["staticRoutesOnly"]
135+
self._state["staticRoutesOnly"] = config.config.staticRoutesOnly
134136

135137
def realize_update_tag(self, allow_recreate):
136-
config = self.get_defn()
137-
tags = config["tags"]
138+
config: AWSVPNConnectionDefinition = self.get_defn()
139+
tags = {k: v for k, v in config.config.tags.items()}
138140
tags.update(self.get_common_tags())
139141
self.get_client().create_tags(
140142
Resources=[self._state["vpnConnectionId"]],

nixops_aws/resources/aws_vpn_connection_route.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ def show_type(self):
3434
class AWSVPNConnectionRouteState(
3535
nixops.resources.DiffEngineResourceState, EC2CommonState
3636
):
37-
"""State of a VPN connection route"""
37+
definition_type = AWSVPNConnectionRouteDefinition
3838

39+
"""State of a VPN connection route"""
3940
state = nixops.util.attr_property(
4041
"state", nixops.resources.DiffEngineResourceState.MISSING, int
4142
)
@@ -79,7 +80,7 @@ def create_after(self, resources, defn):
7980
}
8081

8182
def realize_create_vpn_route(self, allow_recreate):
82-
config = self.get_defn()
83+
config: AWSVPNConnectionRouteDefinition = self.get_defn()
8384

8485
if self.state == self.UP:
8586
if not allow_recreate:
@@ -92,8 +93,8 @@ def realize_create_vpn_route(self, allow_recreate):
9293
self.warn("route definition changed, recreating ...")
9394
self._destroy()
9495

95-
self._state["region"] = config["region"]
96-
vpn_conn_id = config["vpnConnectionId"]
96+
self._state["region"] = config.config.region
97+
vpn_conn_id = config.config.vpnConnectionId
9798
if vpn_conn_id.startswith("res-"):
9899
res = self.depl.get_typed_resource(
99100
vpn_conn_id[4:].split(".")[0],
@@ -104,18 +105,18 @@ def realize_create_vpn_route(self, allow_recreate):
104105

105106
self.log(
106107
"creating route to {0} using vpn connection {1}".format(
107-
config["destinationCidrBlock"], vpn_conn_id
108+
config.config.destinationCidrBlock, vpn_conn_id
108109
)
109110
)
110111
self.get_client().create_vpn_connection_route(
111-
DestinationCidrBlock=config["destinationCidrBlock"],
112+
DestinationCidrBlock=config.config.destinationCidrBlock,
112113
VpnConnectionId=vpn_conn_id,
113114
)
114115

115116
with self.depl._db:
116117
self.state = self.UP
117118
self._state["vpnConnectionId"] = vpn_conn_id
118-
self._state["destinationCidrBlock"] = config["destinationCidrBlock"]
119+
self._state["destinationCidrBlock"] = config.config.destinationCidrBlock
119120

120121
def _destroy(self):
121122
if self.state != self.UP:

nixops_aws/resources/aws_vpn_gateway.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ def show_type(self):
3232
class AWSVPNGatewayState(nixops.resources.DiffEngineResourceState, EC2CommonState):
3333
"""State of a AWS VPN gateway."""
3434

35+
definition_type = AWSVPNGatewayDefinition
36+
3537
state = nixops.util.attr_property(
3638
"state", nixops.resources.DiffEngineResourceState.MISSING, int
3739
)
@@ -74,7 +76,7 @@ def create_after(self, resources, defn):
7476
}
7577

7678
def realize_create_vpn_gtw(self, allow_recreate):
77-
config = self.get_defn()
79+
config: AWSVPNGatewayDefinition = self.get_defn()
7880

7981
if self.state == self.UP:
8082
if not allow_recreate:
@@ -87,17 +89,17 @@ def realize_create_vpn_gtw(self, allow_recreate):
8789
self.warn("VPN gateway changed, recreating...")
8890
self._destroy()
8991

90-
self._state["region"] = config["region"]
91-
vpc_id = config["vpcId"]
92+
self._state["region"] = config.config.region
93+
vpc_id = config.config.vpcId
9294
if vpc_id.startswith("res-"):
9395
res = self.depl.get_typed_resource(
9496
vpc_id[4:].split(".")[0], "vpc", VPCState
9597
)
9698
vpc_id = res._state["vpcId"]
9799

98-
self.log("creating VPN gateway in zone {}".format(config["zone"]))
100+
self.log("creating VPN gateway in zone {}".format(config.config.zone))
99101
response = self.get_client().create_vpn_gateway(
100-
AvailabilityZone=config["zone"], Type="ipsec.1"
102+
AvailabilityZone=config.config.zone, Type="ipsec.1"
101103
)
102104

103105
vpn_gtw_id = response["VpnGateway"]["VpnGatewayId"]
@@ -109,11 +111,11 @@ def realize_create_vpn_gtw(self, allow_recreate):
109111
self.state = self.UP
110112
self._state["vpnGatewayId"] = vpn_gtw_id
111113
self._state["vpcId"] = vpc_id
112-
self._state["zone"] = config["zone"]
114+
self._state["zone"] = config.config.zone
113115

114116
def realize_update_tag(self, allow_recreate):
115-
config = self.get_defn()
116-
tags = config["tags"]
117+
config: AWSVPNGatewayDefinition = self.get_defn()
118+
tags = {k: v for k, v in config.config.tags.items()}
117119
tags.update(self.get_common_tags())
118120
self.get_client().create_tags(
119121
Resources=[self._state["vpnGatewayId"]],

nixops_aws/resources/cloudwatch_log_group.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class CloudWatchLogGroupState(
3333
):
3434
"""State of the cloudwatch log group"""
3535

36+
definition_type = CloudWatchLogGroupDefinition
37+
3638
state = nixops.util.attr_property(
3739
"state", nixops.resources.ResourceState.MISSING, int
3840
)

nixops_aws/resources/cloudwatch_log_stream.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class CloudWatchLogStreamState(
3434
):
3535
"""State of the cloudwatch log group"""
3636

37+
definition_type = CloudWatchLogStreamDefinition
38+
3739
state = nixops.util.attr_property(
3840
"state", nixops.resources.ResourceState.MISSING, int
3941
)

nixops_aws/resources/cloudwatch_metric_alarm.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class CloudwatchMetricAlarmState(
5353
):
5454
"""State of a Cloudwatch Metric Alarm."""
5555

56+
definition_type = CloudwatchMetricAlarmDefinition
57+
5658
state = nixops.util.attr_property(
5759
"state", nixops.resources.ResourceState.MISSING, int
5860
)

nixops_aws/resources/ebs_volume.py

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ def show_type(self):
3232
class EBSVolumeState(nixops.resources.ResourceState, ec2_common.EC2CommonState):
3333
"""State of an EBS volume."""
3434

35+
definition_type = EBSVolumeDefinition
36+
3537
state = nixops.util.attr_property(
3638
"state", nixops.resources.ResourceState.MISSING, int
3739
)
@@ -79,114 +81,118 @@ def _connect_boto3(self, region):
7981
)
8082
return self._conn_boto3
8183

82-
def _get_vol(self, config):
84+
def _get_vol(self, config: EbsVolumeOptions):
8385
try:
84-
_vol = self._connect_boto3(config["region"]).describe_volumes(
85-
VolumeIds=[config["volumeId"]]
86+
_vol = self._connect_boto3(config.region).describe_volumes(
87+
VolumeIds=[config.volumeId]
8688
)["Volumes"][0]
8789
except botocore.exceptions.ClientError as error:
8890
raise error
8991
if _vol["VolumeType"] == "io1":
9092
iops = _vol["Iops"]
9193
else:
92-
iops = config["iops"]
94+
iops = config.iops
9395
with self.depl._db:
9496
self.state = self.STARTING
95-
self.region = config["region"]
97+
self.region = config.region
9698
self.zone = _vol["AvailabilityZone"]
9799
self.size = _vol["Size"]
98-
self.volume_id = config["volumeId"]
100+
self.volume_id = config.volumeId
99101
self.iops = iops
100102
self.volume_type = _vol["VolumeType"]
101103

102-
def create(self, defn, check, allow_reboot, allow_recreate):
104+
def create(self, defn: EBSVolumeDefinition, check, allow_reboot, allow_recreate):
103105

104106
self.access_key_id = (
105-
defn.config["accessKeyId"] or nixops_aws.ec2_utils.get_access_key_id()
107+
defn.config.accessKeyId or nixops_aws.ec2_utils.get_access_key_id()
106108
)
107109
if not self.access_key_id:
108110
raise Exception(
109111
"please set ‘accessKeyId’, $EC2_ACCESS_KEY or $AWS_ACCESS_KEY_ID"
110112
)
111113

112114
if self._exists():
113-
if self.region != defn.config["region"] or self.zone != defn.config["zone"]:
115+
if self.region != defn.config.region or self.zone != defn.config.zone:
114116
raise Exception(
115117
"changing the region or availability zone of an EBS volume is not supported"
116118
)
117119

118-
if defn.config["size"] != 0 and self.size != defn.config["size"]:
120+
if defn.config.size != 0 and self.size != defn.config.size:
119121
raise Exception(
120122
"changing the size an EBS volume is currently not supported"
121123
)
122124

123125
if (
124126
self.volume_type is not None
125-
and defn.config["volumeType"] != self.volume_type
127+
and defn.config.volumeType != self.volume_type
126128
):
127129
raise Exception(
128130
"changing the type of an EBS volume is currently not supported"
129131
)
130132

131-
if defn.config["iops"] != self.iops:
133+
if defn.config.iops != self.iops:
132134
raise Exception(
133135
"changing the IOPS of an EBS volume is currently not supported"
134136
)
135137

136138
if self.state == self.MISSING:
137-
if defn.config["volumeId"]:
139+
if defn.config.volumeId:
138140
self.log(
139-
"Using provided EBS volume ‘{0}’...".format(defn.config["volumeId"])
141+
"Using provided EBS volume ‘{0}’...".format(defn.config.volumeId)
140142
)
141143
self._get_vol(defn.config)
142144
else:
143-
if defn.config["size"] == 0 and defn.config["snapshot"] != "":
144-
snapshots = self._connect(defn.config["region"]).get_all_snapshots(
145-
snapshot_ids=[defn.config["snapshot"]]
145+
if defn.config.size == 0 and defn.config.snapshot != "":
146+
snapshots = self._connect(defn.config.region).get_all_snapshots(
147+
snapshot_ids=[defn.config.snapshot]
146148
)
147149
assert len(snapshots) == 1
148-
defn.config["size"] = snapshots[0].volume_size
150+
defn.config.size = snapshots[0].volume_size
149151

150-
if defn.config["snapshot"]:
152+
if defn.config.snapshot:
151153
self.log(
152154
"creating EBS volume of {0} GiB from snapshot ‘{1}’...".format(
153-
defn.config["size"], defn.config["snapshot"]
155+
defn.config.size, defn.config.snapshot
154156
)
155157
)
156158
else:
157159
self.log(
158-
"creating EBS volume of {0} GiB...".format(defn.config["size"])
160+
"creating EBS volume of {0} GiB...".format(defn.config.size)
159161
)
160162

161-
if defn.config["zone"] is None:
163+
if defn.config.zone is None:
162164
raise Exception(
163165
"please set a zone where the volume will be created"
164166
)
165167

166-
volume = self._connect(defn.config["region"]).create_volume(
167-
zone=defn.config["zone"],
168-
size=defn.config["size"],
169-
snapshot=defn.config["snapshot"],
170-
iops=defn.config["iops"],
171-
volume_type=defn.config["volumeType"],
168+
volume = self._connect(defn.config.region).create_volume(
169+
zone=defn.config.zone,
170+
size=defn.config.size,
171+
snapshot=defn.config.snapshot,
172+
iops=defn.config.iops,
173+
volume_type=defn.config.volumeType,
172174
)
173175
# FIXME: if we crash before the next step, we forget the
174176
# volume we just created. Doesn't seem to be anything we
175177
# can do about this.
176178

177179
with self.depl._db:
178180
self.state = self.STARTING
179-
self.region = defn.config["region"]
180-
self.zone = defn.config["zone"]
181-
self.size = defn.config["size"]
181+
self.region = defn.config.region
182+
self.zone = defn.config.zone
183+
self.size = defn.config.size
182184
self.volume_id = volume.id
183-
self.iops = defn.config["iops"]
184-
self.volume_type = defn.config["volumeType"]
185+
self.iops = defn.config.iops
186+
self.volume_type = defn.config.volumeType
185187

186188
self.log("volume ID is ‘{0}’".format(self.volume_id))
187189

188190
if self.state == self.STARTING or check:
189-
self.update_tags(self.volume_id, user_tags=defn.config["tags"], check=check)
191+
# ensure the connection has been established before calling
192+
# update_tags
193+
self._connect(self.region)
194+
195+
self.update_tags(self.volume_id, user_tags=defn.config.tags, check=check)
190196
nixops_aws.ec2_utils.wait_for_volume_available(
191197
self._connect(self.region),
192198
self.volume_id,

nixops_aws/resources/ec2_common.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from nixops.state import StateDict
1111
from typing import Optional
1212
from boto.ec2.connection import EC2Connection
13-
from typing import TYPE_CHECKING
13+
from typing import Mapping, TYPE_CHECKING
1414

1515
if TYPE_CHECKING:
1616
import mypy_boto3_ec2
@@ -33,7 +33,7 @@ def _retry(self, fun, **kwargs):
3333

3434
tags = nixops.util.attr_property("ec2.tags", {}, "json")
3535

36-
def get_common_tags(self):
36+
def get_common_tags(self) -> Mapping[str, str]:
3737
tags = {
3838
"CharonNetworkUUID": self.depl.uuid,
3939
"CharonMachineName": self.name,
@@ -75,7 +75,7 @@ def get_client(self):
7575
# This class is weird and doesn't have it's full dependencies declared.
7676
# This function will _only_ work when _also_ inheriting from DiffEngineResourceState
7777
new_access_key_id = (
78-
self.get_defn()["accessKeyId"] if self.depl.definitions else None # type: ignore
78+
self.get_defn().config.accessKeyId if self.depl.definitions else None # type: ignore
7979
) or nixops_aws.ec2_utils.get_access_key_id()
8080
if new_access_key_id is not None:
8181
self.access_key_id = new_access_key_id

0 commit comments

Comments
 (0)