Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to update borgbase repositories #53

Merged
merged 2 commits into from
Jan 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 49 additions & 3 deletions library/borgbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,41 @@ def add_repo(key_id):
res = client.execute(REPO_ADD, new_repo_vars)
return res

def get_repo_id(name):
res = client.execute(REPO_DETAILS)
for repo in res['data']['repoList']:
if repo['name'] == name:
repo_id = repo['id']
return repo_id
return None

def edit_repo(repo_id, key_id):
if module.params['append_only']:
access_level = 'appendOnlyKeys'
else:
access_level = 'fullAccessKeys'

if not module.params['quota_enable']:
repo_vars = {
'id': repo_id,
'name': module.params['repository_name'],
access_level: [key_id],
'alertDays': module.params['alertdays'],
'region': module.params['region']
}
else:
repo_vars = {
'id': repo_id,
'name': module.params['repository_name'],
'quotaEnabled': module.params['quota_enable'],
'quota': 1000*module.params['quota'],
access_level: [key_id],
'alertDays': module.params['alertdays'],
'region': module.params['region']
}
res = client.execute(REPO_EDIT, repo_vars)
return res

def main():
global module
module = AnsibleModule(
Expand Down Expand Up @@ -140,8 +175,16 @@ def main():
else:
key_id = get_key_id(module.params['ssh_key'])

# Add new repo using the key
res = add_repo(key_id)
# Check if repo with given name exists
repo_id = get_repo_id(module.params['repository_name'])

if repo_id is None:
# Add new repo using the key
res = add_repo(key_id)

else:
# Edit the repo
res = edit_repo(repo_id, key_id)

# Setup information for Ansible
result = dict(
Expand All @@ -154,7 +197,10 @@ def main():
# Test for success and change info
if type(res) == dict:
result['changed'] = True
result['data'] = res['data']['repoAdd']['repoAdded']
if repo_exist:
result['data'] = res["data"]["repoEdit"]["repoEdited"]
else:
result['data'] = res['data']['repoAdd']['repoAdded']
result['key_id'] = key_id
module.exit_json(**result)
else:
Expand Down