Skip to content

Instantly share code, notes, and snippets.

@scoates
Created September 28, 2015 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scoates/8d229802dcfd7532c135 to your computer and use it in GitHub Desktop.
Save scoates/8d229802dcfd7532c135 to your computer and use it in GitHub Desktop.
_states/
# -*- coding: utf-8 -*-
'''
TODO
'''
# Import Python Libs
from __future__ import absolute_import
import logging
log = logging.getLogger(__name__)
def __virtual__():
'''
Only load if boto is available.
(We use VPC here because it's already available if this is going to work.)
'''
return 'boto_fk' if 'boto_vpc.exists' in __salt__ else False
def subnet_public_ips(name, subnet_id=None, vpc_name=None, vpc_id=None,
region=None, key=None, keyid=None, profile=None):
'''
Ensure subnet has 'Auto Assign Public IP' turned on
name
Name of the VPC.
region
Region to connect to.
key
Secret key to be used.
keyid
Access key to be used.
profile
A dict with region, key and keyid, or a pillar key (string) that
contains a dict with region, key and keyid.
'''
# import needs to be here because of __context__
from salt.utils import boto
ret = {'name': name,
'result': True,
'comment': '',
'changes': {}
}
if vpc_id is None:
vpc_id = __salt__['boto_vpc.check_vpc'](vpc_name=vpc_name)
if not vpc_id:
ret['result'] = False
ret['comment'] = 'Could not find VPC: id={} name={}'.format(vpc_id, vpc_name)
return ret
if subnet_id is None:
subnets_desc = __salt__['boto_vpc.describe_subnets'](
subnet_names=name,
vpc_id=vpc_id
)
if not subnets_desc:
ret['result'] = False
ret['comment'] = 'Could not find subnet: name={}'.format(name)
return ret
subnet_id = subnets_desc['subnets'][0]['id']
# temporary hack to bypass __opts__
region = 'x'
key = 'x'
keyid = 'x'
ec2_conn = boto.get_connection('ec2', region=region, key=key, keyid=keyid, profile=profile)
orig_api_version = ec2_conn.APIVersion
ec2_conn.APIVersion = '2014-06-15'
ret['result'] = ec2_conn.get_status(
'ModifySubnetAttribute',
{'SubnetId': subnet_id, 'MapPublicIpOnLaunch.Value': 'true'},
verb='POST')
ec2_conn.APIVersion = orig_api_version
return ret
@scoates
Copy link
Author

scoates commented Sep 28, 2015

Results in

          ID: opsvpc-subnet0
    Function: boto_fk.subnet_public_ips
      Result: False
     Comment: An exception occurred in this state: Traceback (most recent call last):
                File "/usr/lib/python2.7/dist-packages/salt/state.py", line 1591, in call
                  **cdata['kwargs'])
                File "/var/cache/salt/minion/extmods/states/boto_fk.py", line 77, in subnet_public_ips
                  ec2_conn = boto.get_connection('ec2', region=region, key=key, keyid=keyid, profile=profile)
                File "/usr/lib/python2.7/dist-packages/salt/utils/boto.py", line 194, in get_connection
                  if cxkey in __context__:
              NameError: global name '__context__' is not defined
     Started: 17:32:10.049288
    Duration: 156.706 ms
     Changes:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment