Esempio n. 1
0
    def test_write_or_replace_resource_existing_file(self):
        with utils.tmp_dir_context() as tmp_dir:
            file_path = path.join(tmp_dir, 'resources.yml')
            with open(file_path, 'w') as f:
                f.write(yaml.dump(fixtures.minimal_resource_file_struct()))

            minimal2 = fixtures.MinimalResource.from_data(
                fixtures.valid_minimalresource_data())
            minimal2.data[const.RES_PARAMS]['name'] = 'minimal2'
            minimal2.data[const.RES_PARAMS]['description'] = 'minimal two'
            minimal2.data[const.RES_INFO]['id'] = 'id-minimal2'
            self.assertTrue(
                filesystem.write_or_replace_resource(file_path, minimal2))
            # repeated replacement should report no changes - return False
            self.assertFalse(
                filesystem.write_or_replace_resource(file_path, minimal2))

            file_struct = filesystem.load_resources_file(file_path)
            resource0 = file_struct['resources'][0]
            resource1 = file_struct['resources'][1]
            self.assertEqual(resource0['type'], 'openstack.Minimal')
            self.assertEqual(resource0['params']['name'], 'minimal')
            self.assertEqual(resource0['_info']['id'], 'id-minimal')
            self.assertEqual(
                resource0['params']['description'], 'minimal resource')
            self.assertEqual(resource1['type'], 'openstack.Minimal')
            self.assertEqual(resource1['params']['name'], 'minimal2')
            self.assertEqual(resource1['_info']['id'], 'id-minimal2')
            self.assertEqual(
                resource1['params']['description'], 'minimal two')
Esempio n. 2
0
    def test_write_or_replace_resource_new_file(self):
        with utils.tmp_dir_context() as tmp_dir:
            file_path = path.join(tmp_dir, 'resources.yml')
            filesystem.write_or_replace_resource(file_path,
                                                 fixtures.minimal_resource())

            file_struct = filesystem.load_resources_file(file_path)
            resource = file_struct['resources'][0]
            self.assertEqual(resource['type'], 'openstack.Minimal')
            self.assertEqual(resource['params']['name'], 'minimal')
            self.assertEqual(resource['params']['description'],
                             'minimal resource')
def run_module():
    argument_spec = openstack_full_argument_spec(
        path=dict(type='str', required=True),
        name=dict(type='str', required=True),
    )
    # TODO: check the del
    # del argument_spec['cloud']

    result = dict(changed=False, )

    module = AnsibleModule(
        argument_spec=argument_spec,
        # TODO: Consider check mode. We'd fetch the resource and check
        # if the file representation matches it.
        # supports_check_mode=True,
    )

    sdk, conn = openstack_cloud_from_module(module)
    sdk_rtr = conn.network.find_router(module.params['name'],
                                       ignore_missing=False)
    sdk_ports = router_interface.router_interfaces(conn, sdk_rtr)
    ifaces = map(
        lambda port: router_interface.RouterInterface.from_sdk(conn, port),
        sdk_ports)

    for iface in list(ifaces):
        if filesystem.write_or_replace_resource(module.params['path'], iface):
            result['changed'] = True

    module.exit_json(**result)
Esempio n. 4
0
def run_module():
    argument_spec = openstack_full_argument_spec(
        path=dict(type='str', required=True),
        name=dict(type='str', required=True),
    )
    # TODO: check the del
    # del argument_spec['cloud']

    result = dict(changed=False, )

    module = AnsibleModule(
        argument_spec=argument_spec,
        # TODO: Consider check mode. We'd fetch the resource and check
        # if the file representation matches it.
        # supports_check_mode=True,
    )

    sdk, conn = openstack_cloud_from_module(module)
    sdk_sec = conn.network.find_security_group(module.params['name'],
                                               ignore_missing=False)
    sdk_rules = conn.network.security_group_rules(
        security_group_id=sdk_sec['id'])

    result['changed'] = False

    for sdk_rule in sdk_rules:
        ser_rule = security_group_rule.SecurityGroupRule.from_sdk(
            conn, sdk_rule)

        rchanged = filesystem.write_or_replace_resource(
            module.params['path'], ser_rule)
        if rchanged:
            result['changed'] = True

    module.exit_json(**result)
Esempio n. 5
0
def run_module():
    argument_spec = openstack_full_argument_spec(
        path=dict(type='str', required=True),
        name=dict(type='str', required=True),
    )
    # TODO: check the del
    # del argument_spec['cloud']

    result = dict(changed=False, )

    module = AnsibleModule(
        argument_spec=argument_spec,
        # TODO: Consider check mode. We'd fetch the resource and check
        # if the file representation matches it.
        # supports_check_mode=True,
    )

    sdk, conn = openstack_cloud_from_module(module)
    sdk_image = conn.image.find_image(module.params['name'],
                                      ignore_missing=False)
    ser_image = image.Image.from_sdk(conn, sdk_image)

    result['changed'] = filesystem.write_or_replace_resource(
        module.params['path'], ser_image)

    module.exit_json(**result)
Esempio n. 6
0
def run_module():
    argument_spec = openstack_full_argument_spec(
        path=dict(type='str', required=True),
        name=dict(type='str', required=True),
        migration_params=dict(type='dict', required=False, default={}),
    )
    # TODO: check the del
    # del argument_spec['cloud']

    result = dict(changed=False, )

    module = AnsibleModule(
        argument_spec=argument_spec,
        # TODO: Consider check mode. We'd fetch the resource and check
        # if the file representation matches it.
        # supports_check_mode=True,
    )

    sdk, conn = openstack_cloud_from_module(module)
    sdk_server_nodetails = conn.compute.find_server(module.params['name'],
                                                    ignore_missing=False)
    sdk_server = conn.compute.get_server(sdk_server_nodetails['id'])
    srv = server.Server.from_sdk(conn, sdk_server)
    srv.update_migration_params(module.params['migration_params'])

    result['changed'] = filesystem.write_or_replace_resource(
        module.params['path'], srv)

    module.exit_json(**result)
Esempio n. 7
0
def run_module():
    argument_spec = openstack_full_argument_spec(
        path=dict(type='str', required=True),
        name=dict(type='str', required=True),
    )
    # TODO: check the del
    # del argument_spec['cloud']

    result = dict(changed=False, )

    module = AnsibleModule(
        argument_spec=argument_spec,
        # TODO: Consider check mode. We'd fetch the resource and check
        # if the file representation matches it.
        # supports_check_mode=True,
    )

    sdk, conn = openstack_cloud_from_module(module)
    sdk_sec = conn.network.find_security_group(module.params['name'],
                                               ignore_missing=False)

    result['changed'] = False

    for rule in sdk_sec['security_group_rules']:
        # In this particular case we are creating a SecurityGroupRule
        # object parsed from the rule dictionary.
        # We check that serialize_security_group_rule receives
        # an openstack.network.v2.security_group_rule.SecurityGroupRule
        sec_rule_obj = sdk.network.v2.security_group_rule.SecurityGroupRule(
            **rule)
        # sec_refs = security_group_rule.security_group_rule_refs_from_sdk(conn, sec_rule_obj)
        # ser_sec = security_group_rule.serialize_security_group_rule(sec_rule_obj, sec_refs)
        ser_sec = security_group_rule.SecurityGroupRule.from_sdk(
            conn, sec_rule_obj)

        rchanged = filesystem.write_or_replace_resource(
            module.params['path'], ser_sec)
        if rchanged:
            result['changed'] = True

    module.exit_json(**result)