File: deploy_key.py

package info (click to toggle)
python-bitbucket-api 0.5.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 128 kB
  • sloc: python: 661; makefile: 4
file content (60 lines) | stat: -rw-r--r-- 2,727 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
# -*- coding: utf-8 -*-
URLS = {
    # deploy keys
    'GET_DEPLOY_KEYS': 'repositories/%(username)s/%(repo_slug)s/deploy-keys',
    'SET_DEPLOY_KEY': 'repositories/%(username)s/%(repo_slug)s/deploy-keys',
    'GET_DEPLOY_KEY': 'repositories/%(username)s/%(repo_slug)s/deploy-keys/%(key_id)s',
    'DELETE_DEPLOY_KEY': 'repositories/%(username)s/%(repo_slug)s/deploy-keys/%(key_id)s',
}


class DeployKey(object):
    """ This class provide services-related methods to Bitbucket objects."""

    def __init__(self, bitbucket):
        self.bitbucket = bitbucket
        self.bitbucket.URLS.update(URLS)

    def all(self, repo_slug=None):
        """ Get all ssh keys associated with a repo
        """
        repo_slug = repo_slug or self.bitbucket.repo_slug or ''
        url = self.bitbucket.url('GET_DEPLOY_KEYS',
                                 username=self.bitbucket.username,
                                 repo_slug=repo_slug)
        return self.bitbucket.dispatch('GET', url, auth=self.bitbucket.auth)

    def get(self, repo_slug=None, key_id=None):
        """ Get one of the ssh keys associated with this repo
        """
        repo_slug = repo_slug or self.bitbucket.repo_slug or ''
        url = self.bitbucket.url('GET_DEPLOY_KEY',
                                 key_id=key_id,
                                 username=self.bitbucket.username,
                                 repo_slug=repo_slug)
        return self.bitbucket.dispatch('GET', url, auth=self.bitbucket.auth)

    def create(self, repo_slug=None, key=None, label=None):
        """ Associate an ssh key with your repo and return it.
        """
        key = '%s' % key
        repo_slug = repo_slug or self.bitbucket.repo_slug or ''
        url = self.bitbucket.url('SET_DEPLOY_KEY',
                                 username=self.bitbucket.username,
                                 repo_slug=repo_slug)
        return self.bitbucket.dispatch('POST',
                                       url,
                                       auth=self.bitbucket.auth,
                                       key=key,
                                       label=label)

    def delete(self, repo_slug=None, key_id=None):
        """ Delete one of the ssh keys associated with your repo.
            Please use with caution as there is NO confimation and NO undo.
        """
        repo_slug = repo_slug or self.bitbucket.repo_slug or ''
        url = self.bitbucket.url('DELETE_DEPLOY_KEY',
                                 key_id=key_id,
                                 username=self.bitbucket.username,
                                 repo_slug=repo_slug)
        return self.bitbucket.dispatch('DELETE', url, auth=self.bitbucket.auth)