Ejemplo n.º 1
0
def run_module():
    module = AnsibleModule(argument_spec=dict(
        domain=dict(type='str', required=True),
        src=dict(type='path', required=True),
        dest=dict(type='path', required=True),
    ),
                           supports_check_mode=True)
    connection = Connection(module._socket_path)
    result = {}
    result['changed'] = False
    domain = module.params.get('domain')
    src = module.params.get('src')
    dest = module.params.get('dest')

    file_name = posixpath.split(src)[1]

    if os.path.isdir(dest):
        full_path = posixpath.join(dest, file_name)
    else:
        module.fail_json(msg='No such directory {dest}'.format(dest=dest))

    dp_req = FileRequest()
    dp_req.set_path(domain=domain, file_path=src)

    try:
        dp_resp = connection.send_request(**dp_req.get())
    except ConnectionError as e:
        if 'Resource not found.' in to_text(e):
            msg = 'No such file {src}'.format(src=src)
        else:
            msg = to_text(e)
        module.fail_json(msg=msg)

    tmp_path = posixpath.join(module.tmpdir, file_name)

    create_file_from_base64(tmp_path, dp_resp['file'])

    if os.path.isfile(full_path):
        if not filecmp.cmp(tmp_path, full_path):
            if not module.check_mode:
                module.preserved_copy(tmp_path, full_path)
            result['changed'] = True
    elif os.path.isdir(full_path):
        module.fail_json(msg='{full_path}: Is a directory'.format(
            full_path=full_path))
    else:
        if not module.check_mode:
            module.preserved_copy(tmp_path, full_path)
        result['changed'] = True

    result['path'] = full_path

    module.exit_json(**result)
Ejemplo n.º 2
0
 def test_FileRequest_get(self):
     domain = 'default'
     file_path = 'local/dir/subdir/get.js'
     content = 'aGVsbG8gd29ybGQK'
     req = FileRequest()
     req.set_path(domain=domain, file_path=file_path)
     req.set_body(file_path=file_path, content=content)
     assert req.get(
     )['path'] == '/mgmt/filestore/default/local/dir/subdir/get.js'
     assert req.get()['method'] == 'GET'
     assert not req.get()['data']
Ejemplo n.º 3
0
 def test_FileRequest_update(self):
     domain = 'default'
     file_path = 'local/dir/subdir/get.js'
     content = 'aGVsbG8gd29ybGQK'
     req = FileRequest()
     req.set_path(domain=domain, file_path=file_path)
     req.set_body(file_path=file_path, content=content)
     assert req.put(
     )['path'] == '/mgmt/filestore/default/local/dir/subdir/get.js'
     assert req.put()['method'] == 'PUT'
     assert req.put()['data'] == {
         'file': {
             'name': 'get.js',
             'content': content
         }
     }
Ejemplo n.º 4
0
    def test_FileRequest_set_path_strips_leading_forward_path(self):
        domain = 'default'
        file_path = '/local/dir/subdir/get.js'
        content = 'aGVsbG8gd29ybGQK'
        req = FileRequest()
        req.set_path(domain=domain, file_path=file_path)
        req.set_body(file_path=file_path, content=content)

        assert req.post()['path'] == '/mgmt/filestore/default/local/dir/subdir'
        assert req.post()['method'] == 'POST'
        assert req.post()['data'] == {
            'file': {
                'name': 'get.js',
                'content': content
            }
        }
Ejemplo n.º 5
0
def build_file_request(domain, file_path, data):
    top_dir = file_path.split('/')[0] or posixpath.split(file_path)[0]

    if isBase64(data):
        data_base64 = data
    else:
        data_base64 = base64.b64encode(data).decode()

    file_req = FileRequest()
    # sharedcert is global and can only be used through the default domain.
    if 'sharedcert' in top_dir:
        file_req.set_path(domain='default', file_path=file_path)
    else:
        file_req.set_path(domain=domain, file_path=file_path)
    file_req.set_body(file_path=file_path, content=data_base64)
    return file_req
Ejemplo n.º 6
0
 def test_FileRequest__init__(self):
     domain = 'default'
     file_path = 'local/dir/subdir/get.js'
     content = 'aGVsbG8gd29ybGQK'
     req = FileRequest()
     req.set_path(domain=domain, file_path=file_path)
     req.set_body(file_path=file_path, content=content)
     assert req.path == '/mgmt/filestore/default/local/dir/subdir/get.js'
     assert req.body == {'file': {'name': 'get.js', 'content': content}}