Exemplo n.º 1
0
def get_deployment_type(vsts_url, pat_token):
    rest_call_url = vsts_url + '/_apis/connectiondata'
    response = Util.make_http_call(rest_call_url, 'GET', None, None, pat_token)
    if (response.status == Constants.HTTP_OK):
        connection_data = json.loads(response.read())
        if (connection_data.has_key('deploymentType')):
            return connection_data['deploymentType']
        else:
            return 'onPremises'
    else:
        handler_utility.log(
            'Failed to fetch the connection data for the url {0}. Reason : {1} {2}'
            .format(rest_call_url, str(response.status), response.reason))
        return 'hosted'
Exemplo n.º 2
0
def validate_inputs(config):
    try:
        invalid_pat_error_message = "Please make sure that the Personal Access Token entered is valid and has 'Deployment Groups - Read & manage' scope."
        inputs_validation_error_code = RMExtensionStatus.rm_extension_status[
            'InputConfigurationError']
        unexpected_error_message = "Some unexpected error occured. Status code : {0}"
        error_message_initial_part = "Could not verify that the deployment group '" + config[
            'DeploymentGroup'] + "' exists in the project '" + config[
                'TeamProject'] + "' in the specified organization '" + config[
                    'VSTSUrl'] + "'. Status: {0} Error: {1}. "

        # Verify the deployment group exists and the PAT has the required(Deployment Groups - Read & manage) scope
        # This is the first validation http call, so using Invoke-WebRequest instead of Invoke-RestMethod, because if the PAT provided is not a token at all(not even an unauthorized one) and some random value, then the call
        # would redirect to sign in page and not throw an exception. So, to handle this case.

        specific_error_message = ""
        get_deployment_group_url = "{0}/{1}/_apis/distributedtask/deploymentgroups?name={2}&api-version={3}".format(
            config['VSTSUrl'], quote(config['TeamProject']),
            quote(config['DeploymentGroup']), Constants.projectAPIVersion)

        handler_utility.log(
            "Get deployment group url - {0}".format(get_deployment_group_url))

        response = Util.make_http_call(get_deployment_group_url, 'GET', None,
                                       None, config['PATToken'])

        if (response.status != Constants.HTTP_OK):
            if (response.status == Constants.HTTP_FOUND):
                specific_error_message = invalid_pat_error_message
            elif (response.status == Constants.HTTP_UNAUTHORIZED):
                specific_error_message = invalid_pat_error_message
            elif (response.status == Constants.HTTP_FORBIDDEN):
                specific_error_message = "Please ensure that the user has 'View project-level information' permissions on the project '{0}'.".format(
                    config['TeamProject'])
            elif (response.status == Constants.HTTP_NOTFOUND):
                specific_error_message = "Please make sure that you enter the correct organization name and verify that the project exists in the organization."
            else:
                specific_error_message = unexpected_error_message.format(
                    response.status)
                inputs_validation_error_code = RMExtensionStatus.rm_extension_status[
                    'GenericError']
            error_message = error_message_initial_part.format(
                response.status, specific_error_message)

            raise RMExtensionStatus.new_handler_terminating_error(
                inputs_validation_error_code, error_message)

        deployment_group_data = json.loads(response.read())

        if (('value' not in deployment_group_data)
                or len(deployment_group_data['value']) == 0):
            specific_error_message = "Please make sure that the deployment group {0} exists in the project {1}, and the user has 'Manage' permissions on the deployment group.".format(
                config['DeploymentGroup'], config['TeamProject'])
            raise RMExtensionStatus.new_handler_terminating_error(
                inputs_validation_error_code,
                error_message_initial_part.format(response.status,
                                                  specific_error_message))

        deployment_group_id = deployment_group_data['value'][0]['id']
        handler_utility.log(
            "Validated that the deployment group {0} exists".format(
                config['DeploymentGroup']))

        headers = {}
        headers['Content-Type'] = 'application/json'
        body = "{'name': '" + config['DeploymentGroup'] + "'}"
        patch_deployment_group_url = "{0}/{1}/_apis/distributedtask/deploymentgroups/{2}?api-version={3}".format(
            config['VSTSUrl'], quote(config['TeamProject']),
            deployment_group_id, Constants.projectAPIVersion)

        handler_utility.log("Patch deployment group url - {0}".format(
            patch_deployment_group_url))
        response = Util.make_http_call(patch_deployment_group_url, 'PATCH',
                                       body, headers, config['PATToken'])

        if (response.status != Constants.HTTP_OK):
            if (response.status == Constants.HTTP_FORBIDDEN):
                specific_error_message = "Please ensure that the user has 'Manage' permissions on the deployment group {0}".format(
                    config['DeploymentGroup'])
            else:
                specific_error_message = unexpected_error_message.format(
                    str(response.status))
                inputs_validation_error_code = RMExtensionStatus.rm_extension_status[
                    'GenericError']

            raise RMExtensionStatus.new_handler_terminating_error(
                inputs_validation_error_code,
                error_message_initial_part.format(
                    response.status, response.reason) + specific_error_message)

        handler_utility.log(
            "Validated that the user has 'Manage' permissions on the deployment group '{0}'"
            .format(config['DeploymentGroup']))
        handler_utility.log("Done validating inputs...")
        handler_utility.add_handler_sub_status(
            Util.HandlerSubStatus('SuccessfullyValidatedInputs'))

    except Exception as e:
        set_error_status_and_error_exit(
            e, RMExtensionStatus.rm_extension_status['ValidatingInputs']
            ['operationName'], getattr(e, 'Code'))