def _connect_to_region(self, **kwargs): if self._isRegionInfo: return ElastiCacheConnection(aws_access_key_id=self.aws_access_key_id, aws_secret_access_key=self.aws_secret_access_key, **kwargs) for region in get_regions(self.name): if region.name == self.region: self.region = region return ElastiCacheConnection(aws_access_key_id=self.aws_access_key_id, aws_secret_access_key=self.aws_secret_access_key, region=self.region, **kwargs)
def connect(self): """Connect to API if not already connected; set self.conn.""" if self.conn is not None: return elif self.region: self.conn = self.connect_via(boto.elasticache.connect_to_region) else: self.conn = ElastiCacheConnection()
def connect(self): """Connect to API if not already connected; set self.conn.""" if self.conn is not None: return elif self.region: self.conn = self.connect_via(boto.elasticache.connect_to_region) else: self.conn = ElastiCacheConnection()
def _get_elasticache_connection(self): """Get an elasticache connection""" try: endpoint = "elasticache.%s.amazonaws.com" % self.region connect_region = RegionInfo(name=self.region, endpoint=endpoint) return ElastiCacheConnection(region=connect_region, **self.aws_connect_kwargs) except boto.exception.NoAuthHandlerFound, e: self.module.fail_json(msg=e.message)
def main(): argument_spec = ec2_argument_spec() argument_spec.update( dict( state=dict(required=True, choices=['present', 'absent']), name=dict(required=True), description=dict(required=False), subnets=dict(required=False, type='list'), )) module = AnsibleModule(argument_spec=argument_spec) if not HAS_BOTO: module.fail_json(msg='boto required for this module') state = module.params.get('state') group_name = module.params.get('name').lower() group_description = module.params.get('description') group_subnets = module.params.get('subnets') or {} if state == 'present': for required in ['name', 'description', 'subnets']: if not module.params.get(required): module.fail_json( msg=str("Parameter %s required for state='present'" % required)) else: for not_allowed in ['description', 'subnets']: if module.params.get(not_allowed): module.fail_json( msg=str("Parameter %s not allowed for state='absent'" % not_allowed)) # Retrieve any AWS settings from the environment. region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module) if not region: module.fail_json(msg=str( "Either region or AWS_REGION or EC2_REGION environment variable or boto config aws_region or ec2_region must be set." )) """Get an elasticache connection""" try: endpoint = "elasticache.%s.amazonaws.com" % region connect_region = RegionInfo(name=region, endpoint=endpoint) conn = ElastiCacheConnection(region=connect_region, **aws_connect_kwargs) except boto.exception.NoAuthHandlerFound, e: module.fail_json(msg=e.message)
def main(): argument_spec = ec2_argument_spec() argument_spec.update(dict( state = dict(required=True, choices=['present', 'absent']), name = dict(required=True), description = dict(required=False), subnets = dict(required=False, type='list'), ) ) module = AnsibleModule(argument_spec=argument_spec) if not HAS_BOTO: module.fail_json(msg='boto required for this module') state = module.params.get('state') group_name = module.params.get('name').lower() group_description = module.params.get('description') group_subnets = module.params.get('subnets') or {} if state == 'present': for required in ['name', 'description', 'subnets']: if not module.params.get(required): module.fail_json(msg = str("Parameter %s required for state='present'" % required)) else: for not_allowed in ['description', 'subnets']: if module.params.get(not_allowed): module.fail_json(msg = str("Parameter %s not allowed for state='absent'" % not_allowed)) # Retrieve any AWS settings from the environment. region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module) if not region: module.fail_json(msg = str("Either region or AWS_REGION or EC2_REGION environment variable or boto config aws_region or ec2_region must be set.")) """Get an elasticache connection""" try: endpoint = "elasticache.%s.amazonaws.com" % region connect_region = RegionInfo(name=region, endpoint=endpoint) conn = ElastiCacheConnection(region=connect_region, **aws_connect_kwargs) except boto.exception.NoAuthHandlerFound as e: module.fail_json(msg=e.message) try: changed = False exists = False try: matching_groups = conn.describe_cache_subnet_groups(group_name, max_records=100) exists = len(matching_groups) > 0 except BotoServerError as e: if e.error_code != 'CacheSubnetGroupNotFoundFault': module.fail_json(msg = e.error_message) if state == 'absent': if exists: conn.delete_cache_subnet_group(group_name) changed = True else: if not exists: new_group = conn.create_cache_subnet_group(group_name, cache_subnet_group_description=group_description, subnet_ids=group_subnets) changed = True else: changed_group = conn.modify_cache_subnet_group(group_name, cache_subnet_group_description=group_description, subnet_ids=group_subnets) changed = True except BotoServerError as e: if e.error_message != 'No modifications were requested.': module.fail_json(msg = e.error_message) else: changed = False module.exit_json(changed=changed)
def connect(self): if self.conn is None: logger.debug("Connecting to %s", self.service_name) self.conn = ElastiCacheConnection() logger.info("Connected to %s", self.service_name)
class _ElastiCacheService(_AwsService): service_name = 'ElastiCache' def connect(self): if self.conn is None: logger.debug("Connecting to %s", self.service_name) self.conn = ElastiCacheConnection() logger.info("Connected to %s", self.service_name) def find_usage(self): """ Determine the current usage for each limit of this service, and update corresponding Limit via :py:meth:`~.AwsLimit._add_current_usage`. """ logger.debug("Checking usage for service %s", self.service_name) self.connect() for lim in self.limits.values(): lim._reset_usage() self._find_usage_nodes() self._find_usage_subnet_groups() self._find_usage_parameter_groups() self._find_usage_security_groups() self._have_usage = True logger.debug("Done checking usage.") def _find_usage_nodes(self): """find usage for cache nodes""" nodes = 0 clusters = self.conn.describe_cache_clusters(show_cache_node_info=True)[ 'DescribeCacheClustersResponse']['DescribeCacheClustersResult'][ 'CacheClusters'] for cluster in clusters: nodes += len(cluster['CacheNodes']) self.limits['Nodes per Cluster']._add_current_usage( len(cluster['CacheNodes']), aws_type='AWS::ElastiCache::CacheCluster', resource_id=cluster['CacheClusterId'], ) self.limits['Clusters']._add_current_usage( len(clusters), aws_type='AWS::ElastiCache::CacheCluster' ) self.limits['Nodes']._add_current_usage( nodes, aws_type='AWS::ElastiCache::CacheNode' ) def _find_usage_subnet_groups(self): """find usage for elasticache subnet groups""" groups = self.conn.describe_cache_subnet_groups()[ 'DescribeCacheSubnetGroupsResponse'][ 'DescribeCacheSubnetGroupsResult'][ 'CacheSubnetGroups'] self.limits['Subnet Groups']._add_current_usage( len(groups), aws_type='AWS::ElastiCache::SubnetGroup' ) def _find_usage_parameter_groups(self): """find usage for elasticache parameter groups""" groups = self.conn.describe_cache_parameter_groups()[ 'DescribeCacheParameterGroupsResponse'][ 'DescribeCacheParameterGroupsResult'][ 'CacheParameterGroups'] self.limits['Parameter Groups']._add_current_usage( len(groups), aws_type='AWS::ElastiCache::ParameterGroup' ) def _find_usage_security_groups(self): """find usage for elasticache security groups""" groups = self.conn.describe_cache_security_groups()[ 'DescribeCacheSecurityGroupsResponse'][ 'DescribeCacheSecurityGroupsResult'][ 'CacheSecurityGroups'] self.limits['Security Groups']._add_current_usage( len(groups), aws_type='WS::ElastiCache::SecurityGroup' ) def get_limits(self): """ Return all known limits for this service, as a dict of their names to :py:class:`~.AwsLimit` objects. :returns: dict of limit names to :py:class:`~.AwsLimit` objects :rtype: dict """ if self.limits != {}: return self.limits limits = {} limits['Nodes'] = AwsLimit( 'Nodes', self, 50, self.warning_threshold, self.critical_threshold, limit_type='AWS::ElastiCache::CacheNode', ) limits['Clusters'] = AwsLimit( 'Clusters', self, 50, self.warning_threshold, self.critical_threshold, limit_type='AWS::ElastiCache::CacheCluster', ) limits['Nodes per Cluster'] = AwsLimit( 'Nodes per Cluster', self, 20, self.warning_threshold, self.critical_threshold, limit_type='AWS::ElastiCache::CacheNode', ) limits['Subnet Groups'] = AwsLimit( 'Subnet Groups', self, 50, self.warning_threshold, self.critical_threshold, limit_type='AWS::ElastiCache::SubnetGroup', ) limits['Parameter Groups'] = AwsLimit( 'Parameter Groups', self, 20, self.warning_threshold, self.critical_threshold, limit_type='AWS::ElastiCache::ParameterGroup', ) limits['Security Groups'] = AwsLimit( 'Security Groups', self, 50, self.warning_threshold, self.critical_threshold, limit_type='WS::ElastiCache::SecurityGroup', ) self.limits = limits return limits def required_iam_permissions(self): """ Return a list of IAM Actions required for this Service to function properly. All Actions will be shown with an Effect of "Allow" and a Resource of "*". :returns: list of IAM Action strings :rtype: list """ return [ "elasticache:DescribeCacheClusters", "elasticache:DescribeCacheParameterGroups", "elasticache:DescribeCacheSecurityGroups", "elasticache:DescribeCacheSubnetGroups", ]
class _ElastiCacheService(_AwsService): service_name = 'ElastiCache' def connect(self): """Connect to API if not already connected; set self.conn.""" if self.conn is not None: return elif self.region: self.conn = self.connect_via(boto.elasticache.connect_to_region) else: self.conn = ElastiCacheConnection() def find_usage(self): """ Determine the current usage for each limit of this service, and update corresponding Limit via :py:meth:`~.AwsLimit._add_current_usage`. """ logger.debug("Checking usage for service %s", self.service_name) self.connect() for lim in self.limits.values(): lim._reset_usage() self._find_usage_nodes() self._find_usage_subnet_groups() self._find_usage_parameter_groups() self._find_usage_security_groups() self._have_usage = True logger.debug("Done checking usage.") def _find_usage_nodes(self): """find usage for cache nodes""" nodes = 0 clusters = self.conn.describe_cache_clusters(show_cache_node_info=True)[ 'DescribeCacheClustersResponse']['DescribeCacheClustersResult'][ 'CacheClusters'] for cluster in clusters: nodes += len(cluster['CacheNodes']) self.limits['Nodes per Cluster']._add_current_usage( len(cluster['CacheNodes']), aws_type='AWS::ElastiCache::CacheCluster', resource_id=cluster['CacheClusterId'], ) self.limits['Clusters']._add_current_usage( len(clusters), aws_type='AWS::ElastiCache::CacheCluster' ) self.limits['Nodes']._add_current_usage( nodes, aws_type='AWS::ElastiCache::CacheNode' ) def _find_usage_subnet_groups(self): """find usage for elasticache subnet groups""" groups = self.conn.describe_cache_subnet_groups()[ 'DescribeCacheSubnetGroupsResponse'][ 'DescribeCacheSubnetGroupsResult'][ 'CacheSubnetGroups'] self.limits['Subnet Groups']._add_current_usage( len(groups), aws_type='AWS::ElastiCache::SubnetGroup' ) def _find_usage_parameter_groups(self): """find usage for elasticache parameter groups""" groups = self.conn.describe_cache_parameter_groups()[ 'DescribeCacheParameterGroupsResponse'][ 'DescribeCacheParameterGroupsResult'][ 'CacheParameterGroups'] self.limits['Parameter Groups']._add_current_usage( len(groups), aws_type='AWS::ElastiCache::ParameterGroup' ) def _find_usage_security_groups(self): """find usage for elasticache security groups""" try: # If EC2-Classic isn't available (e.g., a new account) # this method will fail with: # Code: "InvalidParameterValue" # Message: "Use of cache security groups is not permitted in # this API version for your account." # Type: "Sender" groups = self.conn.describe_cache_security_groups()[ 'DescribeCacheSecurityGroupsResponse'][ 'DescribeCacheSecurityGroupsResult'][ 'CacheSecurityGroups'] except BotoServerError: logger.debug("caught BotoServerError checking ElastiCache security " "groups (account without EC2-Classic?)") groups = [] self.limits['Security Groups']._add_current_usage( len(groups), aws_type='WS::ElastiCache::SecurityGroup' ) def get_limits(self): """ Return all known limits for this service, as a dict of their names to :py:class:`~.AwsLimit` objects. :returns: dict of limit names to :py:class:`~.AwsLimit` objects :rtype: dict """ if self.limits != {}: return self.limits limits = {} limits['Nodes'] = AwsLimit( 'Nodes', self, 50, self.warning_threshold, self.critical_threshold, limit_type='AWS::ElastiCache::CacheNode', ) limits['Clusters'] = AwsLimit( 'Clusters', self, 50, self.warning_threshold, self.critical_threshold, limit_type='AWS::ElastiCache::CacheCluster', ) limits['Nodes per Cluster'] = AwsLimit( 'Nodes per Cluster', self, 20, self.warning_threshold, self.critical_threshold, limit_type='AWS::ElastiCache::CacheNode', ) limits['Subnet Groups'] = AwsLimit( 'Subnet Groups', self, 50, self.warning_threshold, self.critical_threshold, limit_type='AWS::ElastiCache::SubnetGroup', ) limits['Parameter Groups'] = AwsLimit( 'Parameter Groups', self, 20, self.warning_threshold, self.critical_threshold, limit_type='AWS::ElastiCache::ParameterGroup', ) limits['Security Groups'] = AwsLimit( 'Security Groups', self, 50, self.warning_threshold, self.critical_threshold, limit_type='WS::ElastiCache::SecurityGroup', ) self.limits = limits return limits def required_iam_permissions(self): """ Return a list of IAM Actions required for this Service to function properly. All Actions will be shown with an Effect of "Allow" and a Resource of "*". :returns: list of IAM Action strings :rtype: list """ return [ "elasticache:DescribeCacheClusters", "elasticache:DescribeCacheParameterGroups", "elasticache:DescribeCacheSecurityGroups", "elasticache:DescribeCacheSubnetGroups", ]