Example #1
0
def test_session_init_minimal():
    port = 9000
    host = 'foo.bar.baz'

    dd_ses = dc_session.DrydockSession(host, port=port)

    assert dd_ses.base_url == "http://%s:%d/api/" % (host, port)
Example #2
0
def test_client_task_get():
    task = {
        'action': 'deploy_node',
        'result': 'success',
        'parent_task': '444a1a40-7b5b-4b80-8265-cadbb783fa82',
        'subtasks': [],
        'status': 'complete',
        'result_detail': {
            'detail': ['Node cab23-r720-17 deployed']
        },
        'site_name': 'mec_demo',
        'task_id': '1476902c-758b-49c0-b618-79ff3fd15166',
        'node_list': ['cab23-r720-17'],
        'design_id': 'fcf37ba1-4cde-48e5-a713-57439fc6e526'
    }

    host = 'foo.bar.baz'

    responses.add(
        responses.GET,
        "http://%s/api/v1.0/tasks/1476902c-758b-49c0-b618-79ff3fd15166" %
        (host),
        json=task,
        status=200)

    dd_ses = dc_session.DrydockSession(host)
    dd_client = dc_client.DrydockClient(dd_ses)

    task_resp = dd_client.get_task('1476902c-758b-49c0-b618-79ff3fd15166')

    assert task_resp['status'] == task['status']
Example #3
0
    def _setup_drydock_client(self):
        """Setup the drydock client for use by this operator"""
        # Retrieve Endpoint Information
        self.drydock_svc_endpoint = self.endpoints.endpoint_by_name(
            service_endpoint.DRYDOCK)

        LOG.info("Drydock endpoint is %s", self.drydock_svc_endpoint)

        # Parse DryDock Service Endpoint
        drydock_url = urlparse(self.drydock_svc_endpoint)

        # Build a DrydockSession with credentials and target host
        # information.
        # The DrydockSession will care for TCP connection pooling
        # and header management
        dd_session = session.DrydockSession(drydock_url.hostname,
                                            port=drydock_url.port,
                                            auth_gen=self._auth_gen)

        # Raise Exception if we are not able to set up the session
        if not dd_session:
            raise DrydockClientUseFailureException(
                "Failed to set up Drydock Session!")

        # Use the DrydockSession to build a DrydockClient that can
        # be used to make one or more API calls
        self.drydock_client = client.DrydockClient(dd_session)
        # Raise Exception if we are not able to build the client
        if not self.drydock_client:
            raise DrydockClientUseFailureException(
                "Failed to set up Drydock Client!")
        LOG.info("Drydock Session and Client etablished.")
Example #4
0
    def drydock_session_client(self, context):

        # Build a DrydockSession with credentials and target host
        # information.
        logging.info("Build DryDock Session")
        dd_session = session.DrydockSession(self.host, port=self.port,
                                            token=context['svc_token'])

        # Raise Exception if we are not able to get a drydock session
        if dd_session:
            logging.info("Successfully Built DryDock Session")
        else:
            raise AirflowException("Unable to get a Drydock Session")

        # Use session to build a DrydockClient to make one or more API calls
        # The DrydockSession will care for TCP connection pooling
        # and header management
        logging.info("Create DryDock Client")
        dd_client = client.DrydockClient(dd_session)

        # Raise Exception if we are not able to build drydock client
        if dd_client:
            logging.info("Successfully Built DryDock client")
        else:
            raise AirflowException("Unable to Build Drydock Client")

        # Drydock client for XCOM Usage
        return dd_client
Example #5
0
def test_session_init_marker():
    host = 'foo.bar.baz'
    marker = '5f1e08b6-38ec-4a99-9d0f-00d29c4e325b'

    dd_ses = dc_session.DrydockSession(host, marker=marker)

    assert dd_ses.base_url == "http://%s/api/" % (host)
    assert dd_ses.marker == marker
Example #6
0
def test_session_init_fernet_token():
    host = 'foo.bar.baz'
    token = 'gAAAAABU7roWGiCuOvgFcckec-0ytpGnMZDBLG9hA7Hr9qfvdZDHjsak39YN98HXxoYLIqVm19Egku5YR3wyI7heVrOmPNEtmr-fIM1rtahudEdEAPM4HCiMrBmiA1Lw6SU8jc2rPLC7FK7nBCia_BGhG17NVHuQu0S7waA306jyKNhHwUnpsBQ'

    dd_ses = dc_session.DrydockSession(host, token=token)

    assert dd_ses.base_url == "http://%s/api/" % (host)
    assert dd_ses.token == token
Example #7
0
def test_session_init_uuid_token():
    host = 'foo.bar.baz'
    token = '5f1e08b6-38ec-4a99-9d0f-00d29c4e325b'

    dd_ses = dc_session.DrydockSession(host, token=token)

    assert dd_ses.base_url == "http://%s/api/" % (host)
    assert dd_ses.token == token
Example #8
0
def drydock_client():
    """Retreive a Drydock client"""
    # Setup the drydock session
    endpoint = svc_endpoints.get_endpoint(Endpoints.DRYDOCK)
    dd_url = urlparse(endpoint)
    session = dd_session.DrydockSession(dd_url.hostname,
                                        port=dd_url.port,
                                        auth_gen=_auth_gen)
    return dd_client.DrydockClient(session)
def drydock_client():
    """Retreive a Drydock client"""
    # Setup the drydock session
    endpoint = svc_endpoints.get_endpoint(Endpoints.DRYDOCK)
    dd_url = urlparse(endpoint)
    session = dd_session.DrydockSession(
        dd_url.hostname,
        port=dd_url.port,
        auth_gen=_auth_gen,
        timeout=(CONF.requests_config.drydock_client_connect_timeout,
                 CONF.requests_config.drydock_client_read_timeout))
    return dd_client.DrydockClient(session)
Example #10
0
def test_taskcli_blank_nodefilter():
    """If no filter values are specified, node filter should be None."""

    host = 'foo.bar.baz'

    dd_ses = dc_session.DrydockSession(host)
    dd_client = dc_client.DrydockClient(dd_ses)

    action = TaskCreate(
        dd_client, "http://foo.bar", action_name="deploy_nodes")

    assert action.node_filter is None
Example #11
0
def test_client_designs_get():
    design_id = '828e88dc-6a8b-11e7-97ae-080027ef795a'
    responses.add(responses.GET,
                  'http://foo.bar.baz/api/v1.0/designs',
                  json=[design_id],
                  status=200)

    host = 'foo.bar.baz'
    token = '5f1e08b6-38ec-4a99-9d0f-00d29c4e325b'

    dd_ses = dc_session.DrydockSession(host, token=token)
    dd_client = dc_client.DrydockClient(dd_ses)
    design_list = dd_client.get_design_ids()

    assert design_id in design_list
Example #12
0
def test_session_get():
    responses.add(responses.GET,
                  'http://foo.bar.baz/api/v1.0/test',
                  body='okay',
                  status=200)
    host = 'foo.bar.baz'
    token = '5f1e08b6-38ec-4a99-9d0f-00d29c4e325b'
    marker = '40c3eaf6-6a8a-11e7-a4bd-080027ef795a'

    dd_ses = dc_session.DrydockSession(host, token=token, marker=marker)

    resp = dd_ses.get('v1.0/test')
    req = resp.request

    assert req.headers.get('X-Auth-Token', None) == token
    assert req.headers.get('X-Context-Marker', None) == marker
def test_client_validate_design_post():
    validation = {'status': 'success'}

    host = 'foo.bar.baz'

    responses.add(responses.POST,
                  "http://%s/api/v1.0/validatedesign" % (host),
                  json=validation,
                  status=200)

    dd_ses = dc_session.DrydockSession(host)
    dd_client = dc_client.DrydockClient(dd_ses)

    validation_resp = dd_client.validate_design('href-placeholder')

    assert validation_resp['status'] == validation['status']
def test_client_get_nodes_for_filter_post():
    node_list = ['node1', 'node2']

    host = 'foo.bar.baz'

    responses.add(responses.POST,
                  "http://%s/api/v1.0/nodefilter" % (host),
                  json=node_list,
                  status=200)

    dd_ses = dc_session.DrydockSession(host)
    dd_client = dc_client.DrydockClient(dd_ses)

    design_ref = {'ref': 'hello'}
    validation_resp = dd_client.get_nodes_for_filter(design_ref)

    assert 'node1' in validation_resp
    assert 'node2' in validation_resp
def test_session_get_returns_401(*args):
    responses.add(responses.GET,
                  'http://foo.bar.baz/api/v1.0/test',
                  body='okay',
                  status=401)
    host = 'foo.bar.baz'
    token = '5f1e08b6-38ec-4a99-9d0f-00d29c4e325b'
    marker = '40c3eaf6-6a8a-11e7-a4bd-080027ef795a'

    def auth_gen():
        return [('X-Auth-Token', dc_session.KeystoneClient.get_token())]

    dd_ses = dc_session.DrydockSession(host, auth_gen=auth_gen, marker=marker)

    resp = dd_ses.get('v1.0/test')
    req = resp.request

    assert req.headers.get('X-Auth-Token', None) == token
    assert req.headers.get('X-Context-Marker', None) == marker
    assert dc_session.KeystoneClient.get_token.call_count == 2
Example #16
0
def test_client_design_get():
    design = {
        'id': '828e88dc-6a8b-11e7-97ae-080027ef795a',
        'model_type': 'SiteDesign'
    }

    responses.add(
        responses.GET,
        'http://foo.bar.baz/api/v1.0/designs/828e88dc-6a8b-11e7-97ae-080027ef795a',
        json=design,
        status=200)

    host = 'foo.bar.baz'

    dd_ses = dc_session.DrydockSession(host)
    dd_client = dc_client.DrydockClient(dd_ses)

    design_resp = dd_client.get_design('828e88dc-6a8b-11e7-97ae-080027ef795a')

    assert design_resp['id'] == design['id']
    assert design_resp['model_type'] == design['model_type']
Example #17
0
    def drydock_session_client(self, drydock_svc_endpoint):
        # Initialize Variables
        drydock_url = None
        dd_session = None
        dd_client = None

        # Parse DryDock Service Endpoint
        drydock_url = urlparse(drydock_svc_endpoint)

        # Build a DrydockSession with credentials and target host
        # information.
        logging.info("Build DryDock Session")
        dd_session = session.DrydockSession(drydock_url.hostname,
                                            port=drydock_url.port,
                                            auth_gen=self._auth_gen)

        # Raise Exception if we are not able to get a drydock session
        if dd_session:
            logging.info("Successfully Set Up DryDock Session")
        else:
            raise AirflowException("Failed to set up Drydock Session!")

        # Use session to build a DrydockClient to make one or more API calls
        # The DrydockSession will care for TCP connection pooling
        # and header management
        logging.info("Create DryDock Client")
        dd_client = client.DrydockClient(dd_session)

        # Raise Exception if we are not able to build drydock client
        if dd_client:
            logging.info("Successfully Set Up DryDock client")
        else:
            raise AirflowException("Unable to set up Drydock Client!")

        # Drydock client for XCOM Usage
        return dd_client
Example #18
0
def test_session_init_minimal_no_port():
    host = 'foo.bar.baz'

    dd_ses = dc_session.DrydockSession(host)

    assert dd_ses.base_url == "http://%s/api/" % (host)
Example #19
0
    def drydock_base(self, context):
        # Initialize Variables
        drydock_url = None
        dd_session = None

        # Define task_instance
        task_instance = context['task_instance']

        # Set up and retrieve values from xcom
        self.xcom_puller = XcomPuller(self.main_dag_name, task_instance)
        self.action_info = self.xcom_puller.get_action_info()
        self.dc = self.xcom_puller.get_deployment_configuration()

        # Logs uuid of action performed by the Operator
        logging.info("DryDock Operator for action %s", self.action_info['id'])

        # Retrieve information of the server that we want to redeploy if user
        # executes the 'redeploy_server' dag
        # Set node filter to be the server that we want to redeploy
        if self.action_info['dag_id'] == 'redeploy_server':
            self.redeploy_server = (
                self.action_info['parameters']['server-name'])

            if self.redeploy_server:
                logging.info("Server to be redeployed is %s",
                             self.redeploy_server)
                self.node_filter = self.redeploy_server
            else:
                raise AirflowException('Unable to retrieve information of '
                                       'node to be redeployed!')

        # Retrieve Endpoint Information
        self.drydock_svc_endpoint = ucp_service_endpoint(
            self, svc_type=self.drydock_svc_type)

        logging.info("Drydock endpoint is %s", self.drydock_svc_endpoint)

        # Parse DryDock Service Endpoint
        drydock_url = urlparse(self.drydock_svc_endpoint)

        # Build a DrydockSession with credentials and target host
        # information.
        # The DrydockSession will care for TCP connection pooling
        # and header management
        logging.info("Build DryDock Session")
        dd_session = session.DrydockSession(drydock_url.hostname,
                                            port=drydock_url.port,
                                            auth_gen=self._auth_gen)

        # Raise Exception if we are not able to set up the session
        if dd_session:
            logging.info("Successfully Set Up DryDock Session")
        else:
            raise AirflowException("Failed to set up Drydock Session!")

        # Use the DrydockSession to build a DrydockClient that can
        # be used to make one or more API calls
        logging.info("Create DryDock Client")
        self.drydock_client = client.DrydockClient(dd_session)

        # Raise Exception if we are not able to build the client
        if self.drydock_client:
            logging.info("Successfully Set Up DryDock client")
        else:
            raise AirflowException("Failed to set up Drydock Client!")

        # Retrieve DeckHand Endpoint Information
        deckhand_svc_endpoint = ucp_service_endpoint(
            self, svc_type=self.deckhand_svc_type)

        logging.info("Deckhand endpoint is %s", deckhand_svc_endpoint)

        # Retrieve last committed revision id
        committed_revision_id = self.xcom_puller.get_design_version()

        # Form DeckHand Design Reference Path
        # This URL will be used to retrieve the Site Design YAMLs
        deckhand_path = "deckhand+" + deckhand_svc_endpoint
        self.deckhand_design_ref = os.path.join(deckhand_path, "revisions",
                                                str(committed_revision_id),
                                                "rendered-documents")
        if self.deckhand_design_ref:
            logging.info("Design YAMLs will be retrieved from %s",
                         self.deckhand_design_ref)
        else:
            raise AirflowException("Unable to Retrieve Design Reference!")
Example #20
0
def test_blank_session_error():
    with pytest.raises(Exception):
        dd_ses = dc_session.DrydockSession()
Example #21
0
    def run_base(self, context):

        # Logs uuid of action performed by the Operator
        LOG.info("DryDock Operator for action %s", self.action_info['id'])

        # Skip workflow if health checks on Drydock failed and continue-on-fail
        # option is turned on
        if self.xcom_puller.get_check_drydock_continue_on_fail():
            LOG.info(
                "Skipping %s as health checks on Drydock have "
                "failed and continue-on-fail option has been "
                "turned on", self.__class__.__name__)

            # Set continue processing to False
            self.continue_processing = False

            return

        # Retrieve information of the server that we want to redeploy if user
        # executes the 'redeploy_server' dag
        # Set node filter to be the server that we want to redeploy
        if self.action_info['dag_id'] == 'redeploy_server':
            self.redeploy_server = (
                self.action_info['parameters']['server-name'])

            if self.redeploy_server:
                LOG.info("Server to be redeployed is %s", self.redeploy_server)
                self.node_filter = self.redeploy_server
            else:
                raise AirflowException('%s was unable to retrieve the '
                                       'server to be redeployed.' %
                                       self.__class__.__name__)

        # Retrieve Endpoint Information
        self.drydock_svc_endpoint = self.endpoints.endpoint_by_name(
            service_endpoint.DRYDOCK)

        LOG.info("Drydock endpoint is %s", self.drydock_svc_endpoint)

        # Parse DryDock Service Endpoint
        drydock_url = urlparse(self.drydock_svc_endpoint)

        # Build a DrydockSession with credentials and target host
        # information.
        # The DrydockSession will care for TCP connection pooling
        # and header management
        LOG.info("Build DryDock Session")
        dd_session = session.DrydockSession(drydock_url.hostname,
                                            port=drydock_url.port,
                                            auth_gen=self._auth_gen)

        # Raise Exception if we are not able to set up the session
        if dd_session:
            LOG.info("Successfully Set Up DryDock Session")
        else:
            raise DrydockClientUseFailureException(
                "Failed to set up Drydock Session!")

        # Use the DrydockSession to build a DrydockClient that can
        # be used to make one or more API calls
        LOG.info("Create DryDock Client")
        self.drydock_client = client.DrydockClient(dd_session)

        # Raise Exception if we are not able to build the client
        if self.drydock_client:
            LOG.info("Successfully Set Up DryDock client")
        else:
            raise DrydockClientUseFailureException(
                "Failed to set up Drydock Client!")