File: extension.py

package info (click to toggle)
azure-devops-cli-extension 1.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,384 kB
  • sloc: python: 160,782; xml: 198; makefile: 56; sh: 51
file content (194 lines) | stat: -rw-r--r-- 7,702 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from knack.log import get_logger
from knack.util import CLIError

from azext_devops.dev.common.services import (get_extension_client,
                                              resolve_instance)

logger = get_logger(__name__)


def search_extensions(search_query):
    """ Search extensions from marketplace
    """
    from msrest.universal_http import ClientRequest
    from msrest.service_client import ServiceClient
    from msrest import Configuration
    from azext_devops.version import VERSION
    config = Configuration(base_url=None)
    config.add_user_agent('devOpsCli/{}'.format(VERSION))
    client = ServiceClient(creds=None, config=config)
    request = ClientRequest(method='POST',
                            url='https://marketplace.visualstudio.com/_apis/public/gallery/extensionquery')

    search_request = {
        'assetTypes': [
            'Microsoft.VisualStudio.Services.Icons.Default',
            'Microsoft.VisualStudio.Services.Icons.Branding',
            'Microsoft.VisualStudio.Services.Icons.Small'
        ],
        'filters': [
            {
                'criteria': [
                    {
                        'filterType': 8,
                        'value': 'Microsoft.VisualStudio.Services'
                    },
                    {
                        'filterType': 8,
                        'value': 'Microsoft.VisualStudio.Services.Integration'
                    },
                    {
                        'filterType': 8,
                        'value': 'Microsoft.VisualStudio.Services.Cloud'
                    },
                    {
                        'filterType': 8,
                        'value': 'Microsoft.TeamFoundation.Server'
                    },
                    {
                        'filterType': 8,
                        'value': 'Microsoft.TeamFoundation.Server.Integration'
                    },
                    {
                        'filterType': 8,
                        'value': 'Microsoft.VisualStudio.Services.Cloud.Integration'
                    },
                    {
                        'filterType': 8,
                        'value': 'Microsoft.VisualStudio.Services.Resource.Cloud'
                    },
                    {
                        'filterType': 10,
                        'value': search_query
                    },
                    {
                        'filterType': 12,
                        'value': '37888'
                    }
                ],
                'direction': 2,
                'pageSize': 50,
                'pageNumber': 1,
                'sortBy': 0,
                'sortOrder': 0,
                'pagingToken': None
            }
        ],
        'flags': 870
    }

    headers = {'Content-Type': 'application/json' + '; charset=utf-8',
               'Accept': 'application/json' + ';api-version=' + '5.0-preview.1'}

    response = client.send(request=request, headers=headers, content=search_request)

    response_json = response.json()
    return response_json['results'][0]['extensions']


def list_extensions(include_built_in=None, include_disabled=None, organization=None, detect=None):
    """ List extensions installed in an organization
    """
    if include_built_in is None:
        include_built_in = True
    if include_disabled is None:
        include_disabled = True
    organization = resolve_instance(detect=detect, organization=organization)
    extension_client = get_extension_client(organization)
    extensions = extension_client.get_installed_extensions(include_disabled_extensions=include_disabled)

    if not include_built_in:
        filteredResult = []
        for extension in extensions:
            if 'builtIn' not in str(extension.flags):
                filteredResult.append(extension)

        extensions = filteredResult

    return extensions


def get_extension(publisher_id, extension_id, organization=None, detect=None):
    """ Get detail of single extension
    """
    organization = resolve_instance(detect=detect, organization=organization)
    extension_client = get_extension_client(organization)
    return extension_client.get_installed_extension_by_name(publisher_name=publisher_id,
                                                            extension_name=extension_id)


def install_extension(publisher_id, extension_id, organization=None, detect=None):
    """ Install an extension
    """
    organization = resolve_instance(detect=detect, organization=organization)
    extension_client = get_extension_client(organization)
    return extension_client.install_extension_by_name(publisher_name=publisher_id,
                                                      extension_name=extension_id)


def uninstall_extension(publisher_id, extension_id, organization=None, detect=None):
    """ Uninstall an extension
    """
    organization = resolve_instance(detect=detect, organization=organization)
    extension_client = get_extension_client(organization)
    return extension_client.uninstall_extension_by_name(publisher_name=publisher_id,
                                                        extension_name=extension_id)


def enable_extension(publisher_id, extension_id, organization=None, detect=None):
    """ Enable an extension
    """
    return _update_extension_state(disable=False,
                                   enable=True,
                                   publisher_id=publisher_id,
                                   extension_id=extension_id,
                                   organization=organization,
                                   detect=detect)


def disable_extension(publisher_id, extension_id, organization=None, detect=None):
    """ Disable an extension
    """
    return _update_extension_state(disable=True,
                                   enable=False,
                                   publisher_id=publisher_id,
                                   extension_id=extension_id,
                                   organization=organization,
                                   detect=detect)


def _update_extension_state(disable, enable,
                            publisher_id, extension_id,
                            organization=None, detect=None):
    organization = resolve_instance(detect=detect, organization=organization)
    extension_client = get_extension_client(organization)
    current_extension = extension_client.get_installed_extension_by_name(
        publisher_name=publisher_id,
        extension_name=extension_id)

    state_from_service = str(current_extension.install_state.flags)
    logger.info('state received from service')
    logger.info(state_from_service)

    if disable:
        flags = [x.strip() for x in state_from_service.split(',')]
        if 'disabled' in flags:
            raise CLIError('Extension is already in disabled state')
        flags.append('disabled')
        updated_state = ', '.join(flags)

    if enable:
        flags = [x.strip() for x in state_from_service.split(',')]
        if 'disabled' not in flags:
            raise CLIError('Extension is already in enabled state')
        flags.remove('disabled')
        updated_state = ', '.join(flags)

    current_extension.install_state.flags = updated_state

    return extension_client.update_installed_extension(current_extension)