def test_parse_invalid_url(bad_url): with pytest.raises(DCOSException): jsonitem._parse_url(bad_url)
def test_parse_url(): assert jsonitem._parse_url('http://test.com:12') == 'http://test.com:12'
def _link(dcos_url, provider_id): """ Link a DC/OS cluster to the current one. :param dcos_url: master ip of the cluster to link to :type dcos_url: str :param provider_id: login provider ID for the linked cluster :type provider_id: str :returns: process status :rtype: int """ current_cluster = cluster.get_attached_cluster() if not current_cluster: raise DCOSException('No cluster is attached, cannot link.') current_cluster_url = current_cluster.get_url() # Accept the same formats as the `setup` command # eg. "my-cluster.example.com" -> "https://my-cluster.example.com" dcos_url = jsonitem._parse_url(dcos_url) try: linked_cluster_ip = socket.gethostbyname(urlparse(dcos_url).netloc) except OSError as error: raise DCOSException("Unable to retrieve IP for '{dcos_url}': {error}" .format(dcos_url=dcos_url, error=error)) # Check if the linked cluster is already configured (based on its IP) for configured_cluster in cluster.get_clusters(): configured_cluster_host = \ urlparse(configured_cluster.get_url()).netloc configured_cluster_ip = socket.gethostbyname(configured_cluster_host) if linked_cluster_ip == configured_cluster_ip: linked_cluster_id = configured_cluster.get_cluster_id() linked_cluster_name = configured_cluster.get_name() break else: msg = ("The cluster you are linking to must be set up locally before\n" "running the `cluster link` command. To set it up now, run:\n" " $ dcos cluster setup {}".format(dcos_url)) raise DCOSException(msg) providers = auth.get_providers(dcos_url) if provider_id: if provider_id not in providers: raise DCOSException( "Incorrect provider ID '{}'.".format(provider_id)) provider_type = providers[provider_id]['authentication-type'] else: (provider_id, provider_type) = _prompt_for_login_provider(providers) message = { 'id': linked_cluster_id, 'name': linked_cluster_name, 'url': dcos_url, 'login_provider': { 'id': provider_id, 'type': provider_type}} headers = { 'Content-Type': 'application/json', 'Accept': 'application/json'} http.post( urllib.parse.urljoin(current_cluster_url, '/cluster/v1/links'), data=json.dumps(message), headers=headers) return 0