Alexander Evseev | 8149449 | 2017-09-14 20:42:08 +0300 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | ''' |
| 3 | Management of artifactory configuration |
| 4 | ======================================= |
| 5 | ''' |
| 6 | |
Alexander Evseev | 245e544 | 2017-11-27 15:23:08 +0100 | [diff] [blame] | 7 | import json |
| 8 | |
Alexander Evseev | 8149449 | 2017-09-14 20:42:08 +0300 | [diff] [blame] | 9 | |
| 10 | def __virtual__(): |
| 11 | if 'artifactory.get_license' in __salt__: |
| 12 | return 'artifactory' |
| 13 | else: |
| 14 | return False, 'Execution module "artifactory" is not loaded' |
| 15 | |
| 16 | |
| 17 | def add_license_key(name, license_key, **kwargs): |
| 18 | |
| 19 | kwargs = __salt__['pillar.get']('artifactory:client:server') |
| 20 | kwargs.pop('license_key', None) |
| 21 | |
| 22 | result, old_license_data = __salt__['artifactory.get_license'](**kwargs) |
| 23 | |
| 24 | result, res_data = __salt__['artifactory.add_license']( |
| 25 | license_key, |
| 26 | **kwargs |
| 27 | ) |
| 28 | |
| 29 | # Prepare data to return |
| 30 | ret = { |
| 31 | 'name': name, |
| 32 | 'changes': {}, |
| 33 | 'result': result, |
| 34 | 'comment': '', |
| 35 | 'pchanges': {}, |
| 36 | } |
| 37 | |
| 38 | if result: |
| 39 | result, new_license_data = __salt__['artifactory.get_license'](**kwargs) |
| 40 | |
| 41 | if old_license_data != new_license_data: |
| 42 | ret['changes'] = { |
| 43 | 'old': json.dumps(old_license_data), |
| 44 | 'new': json.dumps(new_license_data), |
| 45 | } |
| 46 | ret['comment'] = res_data |
| 47 | else: |
| 48 | ret['comment'] = res_data['message'] |
| 49 | if ret['comment'] == ('License could not be installed due ' |
| 50 | 'to an error: License already exists.'): |
| 51 | ret['result'] = True |
| 52 | |
| 53 | return ret |
| 54 | |
| 55 | def configure_ldap(name, uri, base=None, enabled=True, dn_pattern=None, |
| 56 | manager_dn=None, manager_pass=None, search_subtree=True, |
| 57 | search_filter='(&(objectClass=inetOrgPerson)(uid={0}))', |
| 58 | attr_mail='mail', create_users=True, safe_search=True): |
| 59 | |
| 60 | kwargs = __salt__['pillar.get']('artifactory:client:server') |
| 61 | |
| 62 | result, ldap_config_old = __salt__['artifactory.get_ldap_config'](name, **kwargs) |
| 63 | |
| 64 | result, res_data = __salt__['artifactory.set_ldap_config']( |
| 65 | name, uri, base, enabled, dn_pattern, manager_dn, manager_pass, |
| 66 | search_subtree, search_filter, attr_mail, create_users, safe_search, |
| 67 | **kwargs |
| 68 | ) |
| 69 | |
| 70 | # Prepare data to return |
| 71 | ret = { |
| 72 | 'name': name, |
| 73 | 'changes': {}, |
| 74 | 'result': result, |
| 75 | 'comment': '', |
| 76 | 'pchanges': {}, |
| 77 | } |
| 78 | |
| 79 | if result: |
| 80 | result, ldap_config_new = __salt__['artifactory.get_ldap_config'](name, **kwargs) |
| 81 | if ldap_config_old != ldap_config_new: |
| 82 | ret['changes'] = { |
| 83 | 'old': ldap_config_old, |
| 84 | 'new': ldap_config_new, |
| 85 | } |
| 86 | ret['comment'] = res_data |
| 87 | else: |
| 88 | ret['comment'] = res_data.get('errors')[0]['message'] |
| 89 | |
| 90 | return ret |
| 91 | |
| 92 | def configure_repo(name, **kwargs): |
| 93 | |
| 94 | repo_config = kwargs |
| 95 | repo_name = repo_config['key'] |
| 96 | |
| 97 | rclass = repo_config.pop('repo_type', 'local') |
| 98 | if 'rclass' not in repo_config: |
| 99 | repo_config['rclass'] = rclass |
| 100 | |
| 101 | packageType = repo_config.pop('package_type', 'generic') |
| 102 | if 'packageType' not in repo_config: |
| 103 | repo_config['packageType'] = packageType |
| 104 | |
| 105 | kwargs = __salt__['pillar.get']('artifactory:client:server') |
| 106 | |
| 107 | result, repo_config_old = __salt__['artifactory.get_repo'](repo_name, **kwargs) |
| 108 | |
| 109 | # Prepare data to return |
| 110 | ret = { |
| 111 | 'name': name, |
| 112 | 'changes': {}, |
| 113 | 'result': result, |
| 114 | 'comment': '', |
| 115 | 'pchanges': {}, |
| 116 | } |
| 117 | |
| 118 | result, res_data = __salt__['artifactory.set_repo'](repo_name, repo_config, **kwargs) |
| 119 | |
| 120 | if result: |
| 121 | result, repo_config_new = __salt__['artifactory.get_repo'](repo_name, **kwargs) |
| 122 | if repo_config_old != repo_config_new: |
| 123 | ret['changes'] = { |
| 124 | 'old': repo_config_old, |
| 125 | 'new': repo_config_new, |
| 126 | } |
| 127 | ret['comment'] = res_data |
| 128 | else: |
| 129 | ret['comment'] = res_data.get('errors')[0]['message'] |
| 130 | |
| 131 | return ret |