gce_net 9.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#!/usr/bin/python
# Copyright 2013 Google Inc.
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

DOCUMENTATION = '''
---
module: gce_net
22
version_added: "1.5"
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
short_description: create/destroy GCE networks and firewall rules
description:
    - This module can create and destroy Google Compue Engine networks and
      firewall rules U(https://developers.google.com/compute/docs/networking).
      The I(name) parameter is reserved for referencing a network while the
      I(fwname) parameter is used to reference firewall rules.
      IPv4 Address ranges must be specified using the CIDR
      U(http://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing) format.
      Full install/configuration instructions for the gce* modules can
      be found in the comments of ansible/test/gce_tests.py.
options:
  allowed:
    description:
      - the protocol:ports to allow ('tcp:80' or 'tcp:80,443' or 'tcp:80-800')
    required: false
    default: null 
    aliases: []
  ipv4_range:
    description:
      - the IPv4 address range in CIDR notation for the network
    required: false
    aliases: ['cidr']
  fwname:
    description:
      - name of the firewall rule
    required: false
    default: null
    aliases: ['fwrule']
  name:
    description:
      - name of the network
    required: false
    default: null
    aliases: []
  src_range:
    description:
      - the source IPv4 address range in CIDR notation
    required: false
    default: null
    aliases: ['src_cidr']
  src_tags:
    description:
      - the source instance tags for creating a firewall rule
    required: false
    default: null
    aliases: []
  state:
    description:
      - desired state of the persistent disk
    required: false
    default: "present"
    choices: ["active", "present", "absent", "deleted"]
    aliases: []

requirements: [ "libcloud" ]
author: Eric Johnson <erjohnso@google.com>
'''

EXAMPLES = '''
# Simple example of creating a new network
- local_action: 
    module: gce_net
    name: privatenet
    ipv4_range: '10.240.16.0/24'
   
# Simple example of creating a new firewall rule
- local_action: 
    module: gce_net
    name: privatenet
    allowed: tcp:80,8080
    src_tags: ["web", "proxy"]

'''

import sys

Eric Johnson committed
99 100 101
USER_AGENT_PRODUCT="Ansible-gce_net"
USER_AGENT_VERSION="v1beta15"

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
try:                                                                            
    from libcloud.compute.types import Provider                                 
    from libcloud.compute.providers import get_driver                           
    from libcloud.common.google import GoogleBaseError, QuotaExceededError, \
            ResourceExistsError, ResourceNotFoundError
    _ = Provider.GCE                                                            
except ImportError:                                                             
    print("failed=True " + \
            "msg='libcloud with GCE support required for this module.'")
    sys.exit(1)                                                                 
                                                                                
# Load in the libcloud secrets file                                             
try:                                                                            
    import secrets                                                              
except ImportError:                                                             
    secrets = None                                                              
ARGS = getattr(secrets, 'GCE_PARAMS', ())                                       
KWARGS = getattr(secrets, 'GCE_KEYWORD_PARAMS', {})                             

121
if not ARGS or not 'project' in KWARGS:
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    print("failed=True msg='Missing GCE connection " + \
            "parameters in libcloud secrets file.'")
    sys.exit(1)

def unexpected_error_msg(error):
    """Format error string based on passed in error."""
    msg='Unexpected response: HTTP return_code['
    msg+='%s], API error code[%s] and message: %s' % (
            error.http_code, error.code, str(error.value))
    return msg

def format_allowed(allowed):
    """Format the 'allowed' value so that it is GCE compatible."""
    if allowed.count(":") == 0:
        protocol = allowed
        ports = []
    elif allowed.count(":") == 1:
        protocol, ports = allowed.split(":")
    else:
        return []
    if ports.count(","):
        ports = ports.split(",")
    else:
        ports = [ports]
    return_val = {"IPProtocol": protocol}
    if ports:
        return_val["ports"] = ports
    return [return_val]


def main():
    module = AnsibleModule(
        argument_spec = dict(
            allowed = dict(),
            ipv4_range = dict(),
            fwname = dict(),
            name = dict(),
            src_range = dict(),
            src_tags = dict(type='list'),
            state = dict(default='present'),
        )
    )

    allowed = module.params.get('allowed')
    ipv4_range = module.params.get('ipv4_range')
    fwname = module.params.get('fwname')
    name = module.params.get('name')
    src_range = module.params.get('src_range')
    src_tags = module.params.get('src_tags')
    state = module.params.get('state')

    try:
        gce = get_driver(Provider.GCE)(*ARGS, **KWARGS)
Eric Johnson committed
175 176
        gce.connection.user_agent_append("%s/%s" % (
                USER_AGENT_PRODUCT, USER_AGENT_VERSION))
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
    except Exception as e:
        module.fail_json(msg=unexpected_error_msg(e), changed=False)

    changed = False
    json_output = {'state': state}

    if state in ['active', 'present']:
        network = None
        try:
            network = gce.ex_get_network(name)
            json_output['name'] = name
            json_output['ipv4_range'] = network.cidr
        except ResourceNotFoundError:
            pass
        except Exception as e:
            module.fail_json(msg=unexpected_error_msg(e), changed=False)

        # user wants to create a new network that doesn't yet exist
        if name and not network:
            if not ipv4_range:
                module.fail_json(msg="Missing required 'ipv4_range' parameter",
                    changed=False)

            try:
                network = gce.ex_create_network(name, ipv4_range)
                json_output['name'] = name
                json_output['ipv4_range'] = ipv4_range
                changed = True
            except Exception as e:
                module.fail_json(msg=unexpected_error_msg(e), changed=False)

        if fwname:
            # user creating a firewall rule
            if not allowed and not src_range and not src_tags:
                if changed and network:
                    module.fail_json(
                        msg="Network created, but missing required " + \
                        "firewall rule parameter(s)", changed=True)
                module.fail_json(
                    msg="Missing required firewall rule parameter(s)",
                    changed=False)

            allowed_list = format_allowed(allowed)

            try:
                gce.ex_create_firewall(fwname, allowed_list, network=name,
                        source_ranges=src_range, source_tags=src_tags)
                changed = True
            except ResourceExistsError:
                pass
            except Exception as e:
                module.fail_json(msg=unexpected_error_msg(e), changed=False)

            json_output['fwname'] = fwname
            json_output['allowed'] = allowed
            json_output['src_range'] = src_range
            json_output['src_tags'] = src_tags

    if state in ['absent', 'deleted']:
        if fwname:
            json_output['fwname'] = fwname
            fw = None
            try:
                fw = gce.ex_get_firewall(fwname)
            except ResourceNotFoundError:
                pass
            except Exception as e:
                module.fail_json(msg=unexpected_error_msg(e), changed=False)
            if fw:
                gce.ex_destroy_firewall(fw)
                changed = True
        if name:
            json_output['name'] = name
            network = None
            try:
                network = gce.ex_get_network(name)
#                json_output['d1'] = 'found network name %s' % name
            except ResourceNotFoundError:
#                json_output['d2'] = 'not found network name %s' % name
                pass
            except Exception as e:
#                json_output['d3'] = 'error with %s' % name
                module.fail_json(msg=unexpected_error_msg(e), changed=False)
            if network:
#                json_output['d4'] = 'deleting %s' % name
                gce.ex_destroy_network(network)
#                json_output['d5'] = 'deleted %s' % name
                changed = True

    json_output['changed'] = changed
    print json.dumps(json_output)
    sys.exit(0)

270
# import module snippets
271
from ansible.module_utils.basic import *
272 273

main()