def test_run_job(self):
        # Setup Expected Response
        name_2 = "name2-1052831874"
        description = "description-1724546052"
        schedule = "schedule-697920873"
        time_zone = "timeZone36848094"
        expected_response = {
            "name": name_2,
            "description": description,
            "schedule": schedule,
            "time_zone": time_zone,
        }
        expected_response = job_pb2.Job(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = scheduler_v1beta1.CloudSchedulerClient()

        # Setup Request
        name = client.job_path("[PROJECT]", "[LOCATION]", "[JOB]")

        response = client.run_job(name)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = cloudscheduler_pb2.RunJobRequest(name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
コード例 #2
0
    def test_resume_job(self):
        # Setup Expected Response
        name_2 = 'name2-1052831874'
        description = 'description-1724546052'
        schedule = 'schedule-697920873'
        time_zone = 'timeZone36848094'
        expected_response = {
            'name': name_2,
            'description': description,
            'schedule': schedule,
            'time_zone': time_zone
        }
        expected_response = job_pb2.Job(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch('google.api_core.grpc_helpers.create_channel')
        with patch as create_channel:
            create_channel.return_value = channel
            client = scheduler_v1beta1.CloudSchedulerClient()

        # Setup Request
        name = client.job_path('[PROJECT]', '[LOCATION]', '[JOB]')

        response = client.resume_job(name)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = cloudscheduler_pb2.ResumeJobRequest(name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_update_job(self):
        # Setup Expected Response
        name = "name3373707"
        description = "description-1724546052"
        schedule = "schedule-697920873"
        time_zone = "timeZone36848094"
        expected_response = {
            "name": name,
            "description": description,
            "schedule": schedule,
            "time_zone": time_zone,
        }
        expected_response = job_pb2.Job(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = scheduler_v1beta1.CloudSchedulerClient()

        # Setup Request
        job = {}

        response = client.update_job(job)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = cloudscheduler_pb2.UpdateJobRequest(job=job)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_update_job(self):
        # Setup Expected Response
        name = 'name3373707'
        description = 'description-1724546052'
        schedule = 'schedule-697920873'
        time_zone = 'timeZone36848094'
        expected_response = {
            'name': name,
            'description': description,
            'schedule': schedule,
            'time_zone': time_zone
        }
        expected_response = job_pb2.Job(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        client = scheduler_v1beta1.CloudSchedulerClient(channel=channel)

        # Setup Request
        job = {}

        response = client.update_job(job)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = cloudscheduler_pb2.UpdateJobRequest(job=job)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_list_jobs(self):
        # Setup Expected Response
        next_page_token = ''
        jobs_element = {}
        jobs = [jobs_element]
        expected_response = {'next_page_token': next_page_token, 'jobs': jobs}
        expected_response = cloudscheduler_pb2.ListJobsResponse(
            **expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        client = scheduler_v1beta1.CloudSchedulerClient(channel=channel)

        # Setup Request
        parent = client.location_path('[PROJECT]', '[LOCATION]')

        paged_list_response = client.list_jobs(parent)
        resources = list(paged_list_response)
        assert len(resources) == 1

        assert expected_response.jobs[0] == resources[0]

        assert len(channel.requests) == 1
        expected_request = cloudscheduler_pb2.ListJobsRequest(parent=parent)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_run_job(self):
        # Setup Expected Response
        name_2 = 'name2-1052831874'
        description = 'description-1724546052'
        schedule = 'schedule-697920873'
        time_zone = 'timeZone36848094'
        expected_response = {
            'name': name_2,
            'description': description,
            'schedule': schedule,
            'time_zone': time_zone
        }
        expected_response = job_pb2.Job(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        client = scheduler_v1beta1.CloudSchedulerClient(channel=channel)

        # Setup Request
        name = client.job_path('[PROJECT]', '[LOCATION]', '[JOB]')

        response = client.run_job(name)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = cloudscheduler_pb2.RunJobRequest(name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_list_jobs(self):
        # Setup Expected Response
        next_page_token = ""
        jobs_element = {}
        jobs = [jobs_element]
        expected_response = {"next_page_token": next_page_token, "jobs": jobs}
        expected_response = cloudscheduler_pb2.ListJobsResponse(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = scheduler_v1beta1.CloudSchedulerClient()

        # Setup Request
        parent = client.location_path("[PROJECT]", "[LOCATION]")

        paged_list_response = client.list_jobs(parent)
        resources = list(paged_list_response)
        assert len(resources) == 1

        assert expected_response.jobs[0] == resources[0]

        assert len(channel.requests) == 1
        expected_request = cloudscheduler_pb2.ListJobsRequest(parent=parent)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_run_job_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        client = scheduler_v1beta1.CloudSchedulerClient(channel=channel)

        # Setup request
        name = client.job_path('[PROJECT]', '[LOCATION]', '[JOB]')

        with pytest.raises(CustomException):
            client.run_job(name)
    def test_update_job_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        client = scheduler_v1beta1.CloudSchedulerClient(channel=channel)

        # Setup request
        job = {}

        with pytest.raises(CustomException):
            client.update_job(job)
    def test_list_jobs_exception(self):
        channel = ChannelStub(responses=[CustomException()])
        client = scheduler_v1beta1.CloudSchedulerClient(channel=channel)

        # Setup request
        parent = client.location_path('[PROJECT]', '[LOCATION]')

        paged_list_response = client.list_jobs(parent)
        with pytest.raises(CustomException):
            list(paged_list_response)
    def test_create_job_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        client = scheduler_v1beta1.CloudSchedulerClient(channel=channel)

        # Setup request
        parent = client.location_path('[PROJECT]', '[LOCATION]')
        job = {}

        with pytest.raises(CustomException):
            client.create_job(parent, job)
    def test_run_job_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = scheduler_v1beta1.CloudSchedulerClient()

        # Setup request
        name = client.job_path("[PROJECT]", "[LOCATION]", "[JOB]")

        with pytest.raises(CustomException):
            client.run_job(name)
    def test_list_jobs_exception(self):
        channel = ChannelStub(responses=[CustomException()])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = scheduler_v1beta1.CloudSchedulerClient()

        # Setup request
        parent = client.location_path("[PROJECT]", "[LOCATION]")

        paged_list_response = client.list_jobs(parent)
        with pytest.raises(CustomException):
            list(paged_list_response)
    def test_update_job_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = scheduler_v1beta1.CloudSchedulerClient()

        # Setup request
        job = {}

        with pytest.raises(CustomException):
            client.update_job(job)
    def test_delete_job(self):
        channel = ChannelStub()
        client = scheduler_v1beta1.CloudSchedulerClient(channel=channel)

        # Setup Request
        name = client.job_path('[PROJECT]', '[LOCATION]', '[JOB]')

        client.delete_job(name)

        assert len(channel.requests) == 1
        expected_request = cloudscheduler_pb2.DeleteJobRequest(name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
コード例 #16
0
    def test_create_job_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        patch = mock.patch('google.api_core.grpc_helpers.create_channel')
        with patch as create_channel:
            create_channel.return_value = channel
            client = scheduler_v1beta1.CloudSchedulerClient()

        # Setup request
        parent = client.location_path('[PROJECT]', '[LOCATION]')
        job = {}

        with pytest.raises(CustomException):
            client.create_job(parent, job)
    def test_delete_job(self):
        channel = ChannelStub()
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = scheduler_v1beta1.CloudSchedulerClient()

        # Setup Request
        name = client.job_path("[PROJECT]", "[LOCATION]", "[JOB]")

        client.delete_job(name)

        assert len(channel.requests) == 1
        expected_request = cloudscheduler_pb2.DeleteJobRequest(name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
コード例 #18
0
from google.cloud import scheduler_v1beta1

client = scheduler_v1beta1.CloudSchedulerClient()

project = "my-project-id"

location = "europe-west1"

name = "my-job-name"

parent = client.location_path(project, location)

job = {
    "name": "projects/{}/locations/{}/jobs/{}".format(project, location, name),
    "description": "Test",
    "schedule": "* * * * *",
    "http_target": {
        "http_method": "GET",
        "uri": "https://google.com"
    }
}

response = client.create_job(parent, job)
from google.oauth2 import service_account
import googleapiclient.discovery
SCOPES = ['https://www.googleapis.com/auth/cloud-scheduler',
          'https://www.googleapis.com/auth/cloud-platform']

SERVICE_ACCOUNT_FILE = '../../auth/vantage2018-gke-809963f91f38.json'

PROJECT_ID = "vantage2018-gke"
LOCATION_ID = "us-central1"
JOB_NAME = 'test_start_auto_py'
JOB_NAME1 = 'test1'
credentials = service_account.Credentials.from_service_account_file(
    SERVICE_ACCOUNT_FILE, scopes=SCOPES)
print(credentials)


# API REFERENCE :https://googleapis.github.io/google-cloud-python/latest/scheduler/gapic/v1beta1/api.html
# Contruct the client to make Cloud Scheduler calls

client = scheduler_v1beta1.CloudSchedulerClient(credentials=credentials)
parent = client.location_path(PROJECT_ID, LOCATION_ID)
print(parent)
name = client.job_path(PROJECT_ID, LOCATION_ID, JOB_NAME)
print("name: {}".format(name))

# https://stackoverflow.com/questions/46102807/getting-403-error-when-accessing-google-my-business-api-through-service-account


response = client.get_job(name)
print(response)