Skip to content
This repository has been archived by the owner on Dec 31, 2023. It is now read-only.

Commit

Permalink
Remove usage of GoogleCredentials [(#810)](GoogleCloudPlatform/python…
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Wayne Parrott authored Feb 17, 2017
1 parent da388bd commit aeefb56
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
12 changes: 8 additions & 4 deletions samples/snippets/v3/api-client/custom_metric_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@

from gcp.testing import eventually_consistent
from gcp.testing.flaky import flaky
import googleapiclient.discovery
import pytest

from custom_metric import create_custom_metric
from custom_metric import delete_metric_descriptor
from custom_metric import get_custom_metric
from custom_metric import read_timeseries
from custom_metric import write_timeseries_value

import list_resources

""" Custom metric domain for all custom metrics"""
CUSTOM_METRIC_DOMAIN = "custom.googleapis.com"

Expand All @@ -44,10 +44,14 @@
CUSTOM_METRIC_DOMAIN, METRIC_NAME)


@pytest.fixture(scope='module')
def client():
return googleapiclient.discovery.build('monitoring', 'v3')


@flaky
def test_custom_metric(cloud_config):
def test_custom_metric(cloud_config, client):
PROJECT_RESOURCE = "projects/{}".format(cloud_config.project)
client = list_resources.get_client()
# Use a constant seed so psuedo random number is known ahead of time
random.seed(1)
pseudo_random_value = random.randint(0, 10)
Expand Down
14 changes: 3 additions & 11 deletions samples/snippets/v3/api-client/list_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
import datetime
import pprint

from apiclient import discovery
from oauth2client.client import GoogleCredentials
import googleapiclient.discovery


def format_rfc3339(datetime_instance):
Expand Down Expand Up @@ -96,17 +95,10 @@ def list_timeseries(client, project_resource, metric):
print('list_timeseries response:\n{}'.format(pprint.pformat(response)))


def get_client():
"""Builds an http client authenticated with the service account
credentials."""
credentials = GoogleCredentials.get_application_default()
client = discovery.build('monitoring', 'v3', credentials=credentials)
return client


def main(project_id):
client = googleapiclient.discovery.build('monitoring', 'v3')

project_resource = "projects/{}".format(project_id)
client = get_client()
list_monitored_resource_descriptors(client, project_resource)
# Metric to list
metric = 'compute.googleapis.com/instance/cpu/usage_time'
Expand Down
16 changes: 10 additions & 6 deletions samples/snippets/v3/api-client/list_resources_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,22 @@
import re

from gcp.testing.flaky import flaky
import googleapiclient.discovery
import pytest

import list_resources

METRIC = 'compute.googleapis.com/instance/cpu/usage_time'


@pytest.fixture(scope='module')
def client():
return googleapiclient.discovery.build('monitoring', 'v3')


@flaky
def test_list_monitored_resources(cloud_config, capsys):
def test_list_monitored_resources(cloud_config, client, capsys):
PROJECT_RESOURCE = "projects/{}".format(cloud_config.project)
client = list_resources.get_client()
list_resources.list_monitored_resource_descriptors(
client, PROJECT_RESOURCE)
stdout, _ = capsys.readouterr()
Expand All @@ -42,9 +48,8 @@ def test_list_monitored_resources(cloud_config, capsys):


@flaky
def test_list_metrics(cloud_config, capsys):
def test_list_metrics(cloud_config, client, capsys):
PROJECT_RESOURCE = "projects/{}".format(cloud_config.project)
client = list_resources.get_client()
list_resources.list_metric_descriptors(
client, PROJECT_RESOURCE, METRIC)
stdout, _ = capsys.readouterr()
Expand All @@ -54,9 +59,8 @@ def test_list_metrics(cloud_config, capsys):


@flaky
def test_list_timeseries(cloud_config, capsys):
def test_list_timeseries(cloud_config, client, capsys):
PROJECT_RESOURCE = "projects/{}".format(cloud_config.project)
client = list_resources.get_client()
list_resources.list_timeseries(
client, PROJECT_RESOURCE, METRIC)
stdout, _ = capsys.readouterr()
Expand Down

0 comments on commit aeefb56

Please sign in to comment.