def test_create_build(self):
        # Setup Expected Response
        name = "name3373707"
        done = True
        expected_response = {"name": name, "done": done}
        expected_response = operations_pb2.Operation(**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 = cloudbuild_v1.CloudBuildClient()

        # Setup Request
        project_id = "projectId-1969970175"
        build = {}

        response = client.create_build(project_id, build)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = cloudbuild_pb2.CreateBuildRequest(
            project_id=project_id, build=build)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_list_build_triggers(self):
        # Setup Expected Response
        next_page_token = "nextPageToken-1530815211"
        expected_response = {"next_page_token": next_page_token}
        expected_response = cloudbuild_pb2.ListBuildTriggersResponse(
            **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 = cloudbuild_v1.CloudBuildClient()

        # Setup Request
        project_id = "projectId-1969970175"

        response = client.list_build_triggers(project_id)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = cloudbuild_pb2.ListBuildTriggersRequest(
            project_id=project_id)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_list_builds(self):
        # Setup Expected Response
        next_page_token = ""
        builds_element = {}
        builds = [builds_element]
        expected_response = {
            "next_page_token": next_page_token,
            "builds": builds
        }
        expected_response = cloudbuild_pb2.ListBuildsResponse(
            **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 = cloudbuild_v1.CloudBuildClient()

        # Setup Request
        project_id = "projectId-1969970175"

        paged_list_response = client.list_builds(project_id)
        resources = list(paged_list_response)
        assert len(resources) == 1

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

        assert len(channel.requests) == 1
        expected_request = cloudbuild_pb2.ListBuildsRequest(
            project_id=project_id)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_get_build_trigger(self):
        # Setup Expected Response
        id_ = "id3355"
        description = "description-1724546052"
        name = "name3373707"
        filename = "filename-734768633"
        disabled = True
        expected_response = {
            "id": id_,
            "description": description,
            "name": name,
            "filename": filename,
            "disabled": disabled,
        }
        expected_response = cloudbuild_pb2.BuildTrigger(**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 = cloudbuild_v1.CloudBuildClient()

        # Setup Request
        project_id = "projectId-1969970175"
        trigger_id = "triggerId1363517698"

        response = client.get_build_trigger(project_id, trigger_id)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = cloudbuild_pb2.GetBuildTriggerRequest(
            project_id=project_id, trigger_id=trigger_id)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_update_worker_pool(self):
        # Setup Expected Response
        name = "name3373707"
        project_id = "projectId-1969970175"
        service_account_email = "serviceAccountEmail-1300473088"
        worker_count = 372044046
        expected_response = {
            "name": name,
            "project_id": project_id,
            "service_account_email": service_account_email,
            "worker_count": worker_count,
        }
        expected_response = cloudbuild_pb2.WorkerPool(**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 = cloudbuild_v1.CloudBuildClient()

        response = client.update_worker_pool()
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = cloudbuild_pb2.UpdateWorkerPoolRequest()
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Ejemplo n.º 6
0
def daily_automl(event, context):
    from google.cloud.devtools import cloudbuild_v1
    import os
    client = cloudbuild_v1.CloudBuildClient()

    project_id = os.getenv('GOOGLE_CLOUD_PROJ')
    trigger_id = os.getenv('CLOUD_BUILD_TRIGGER_ID')
    source = {'branch_name': os.getenv('BRANCH_NAME')}
    response = client.run_build_trigger(project_id, trigger_id, source)
    def test_list_worker_pools_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 = cloudbuild_v1.CloudBuildClient()

        with pytest.raises(CustomException):
            client.list_worker_pools()
    def invoke_docker_build_and_push(self, container_image_name):
        project_id = self._google_cloud_options.project
        temp_location = self._google_cloud_options.temp_location
        # google cloud build service expects all the build source file to be
        # compressed into a tarball.
        tarball_path = os.path.join(self._temp_src_dir,
                                    '%s.tgz' % SOURCE_FOLDER)
        self._make_tarfile(tarball_path, self._temp_src_dir)
        _LOGGER.info(
            "Compressed source files for building sdk container at %s" %
            tarball_path)

        container_image_tag = container_image_name.split(':')[-1]
        gcs_location = os.path.join(
            temp_location, '%s-%s.tgz' % (SOURCE_FOLDER, container_image_tag))
        self._upload_to_gcs(tarball_path, gcs_location)

        from google.cloud.devtools import cloudbuild_v1
        client = cloudbuild_v1.CloudBuildClient()
        build = cloudbuild_v1.Build()
        build.steps = []
        step = cloudbuild_v1.BuildStep()
        step.name = 'gcr.io/kaniko-project/executor:latest'
        step.args = ['--destination=' + container_image_name, '--cache=true']
        step.dir = SOURCE_FOLDER

        build.steps.append(step)

        source = cloudbuild_v1.Source()
        source.storage_source = cloudbuild_v1.StorageSource()
        gcs_bucket, gcs_object = self._get_gcs_bucket_and_name(gcs_location)
        source.storage_source.bucket = os.path.join(gcs_bucket)
        source.storage_source.object = gcs_object
        build.source = source
        # TODO(zyichi): make timeout configurable
        build.timeout = Duration().FromSeconds(seconds=1800)

        now = time.time()
        operation = client.create_build(project_id=project_id, build=build)
        _LOGGER.info(
            'Building sdk container with Google Cloud Build, this may '
            'take a few minutes, you may check build log at %s' %
            operation.metadata.build.log_url)

        # block until build finish, if build fails exception will be raised and
        # stops the job submission.
        operation.result()

        _LOGGER.info(
            "Python SDK container pre-build finished in %.2f seconds" %
            (time.time() - now))
        _LOGGER.info("Python SDK container built and pushed as %s." %
                     container_image_name)
    def test_delete_worker_pool(self):
        channel = ChannelStub()
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = cloudbuild_v1.CloudBuildClient()

        client.delete_worker_pool()

        assert len(channel.requests) == 1
        expected_request = cloudbuild_pb2.DeleteWorkerPoolRequest()
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Ejemplo n.º 10
0
def trigger_cloud_build(request):
    build = {
        "steps": [{
            "name": "bash",
            "args": ["echo", "Hello Cloud Build"],
        }],
    }
    client = cloudbuild_v1.CloudBuildClient()

    response = client.create_build(
        project_id="PROJECT_ID",
        build=cloudbuild_v1.Build(build),
    )
    def test_list_build_triggers_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 = cloudbuild_v1.CloudBuildClient()

        # Setup request
        project_id = "projectId-1969970175"

        with pytest.raises(CustomException):
            client.list_build_triggers(project_id)
def lambda_handler(event, context):
    obj = s3.Object(bucket, key)
    data = json.load(obj.get()['Body'])

    with open('/tmp/creds.json', 'w') as json_file:
        json.dump(data, json_file, indent=4)

    os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = '/tmp/creds.json'

    client = cloudbuild_v1.CloudBuildClient()
    reposource = cloudbuild_v1.types.RepoSource(project_id=project_id,
                                                repo_name=repo_name,
                                                branch_name=branch_name)
    client.run_build_trigger(project_id=project_id,
                             trigger_id=trigger_id,
                             source=reposource)
Ejemplo n.º 13
0
def trigger_build(event, context):
    from google.cloud.devtools import cloudbuild_v1
    import sys
    import os
    import traceback
    client = cloudbuild_v1.CloudBuildClient()
    project_id = os.environ['PROJECT_ID']
    trigger_id = 'terraform-builder-trigger'
    print(project_id)
    source = {"project_id": project_id, "branch_name": "master"}
    try:
        response = client.run_build_trigger(
          project_id = project_id,
          trigger_id = trigger_id,
          source = source)
        print("Called Trigger")
    except Exception as err:
        traceback.print_tb(err.__traceback__)
    def test_delete_build_trigger(self):
        channel = ChannelStub()
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = cloudbuild_v1.CloudBuildClient()

        # Setup Request
        project_id = "projectId-1969970175"
        trigger_id = "triggerId1363517698"

        client.delete_build_trigger(project_id, trigger_id)

        assert len(channel.requests) == 1
        expected_request = cloudbuild_pb2.DeleteBuildTriggerRequest(
            project_id=project_id, trigger_id=trigger_id)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_list_worker_pools(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = cloudbuild_pb2.ListWorkerPoolsResponse(
            **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 = cloudbuild_v1.CloudBuildClient()

        response = client.list_worker_pools()
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = cloudbuild_pb2.ListWorkerPoolsRequest()
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Ejemplo n.º 16
0
def cve_trigger(event, context):
    """Triggered from a message on a Cloud Pub/Sub topic.
    Args:
         event (dict): Event payload.
         context (google.cloud.functions.Context): Metadata for the event.
    """
    pubsub_message = json.loads(base64.b64decode(event['data']).decode('utf-8'))
    print(pubsub_message)
    
    if "VULNERABILITY" in pubsub_message['kind']:
        occurrence_id = os.path.basename(pubsub_message['name'])

        client = containeranalysis_v1.ContainerAnalysisClient()
        grafeas_client = client.get_grafeas_client()
        parent = grafeas_client.occurrence_path(project_id, occurrence_id)
        occurrence = grafeas_client.get_occurrence(parent)
        if registry in occurrence.resource_uri:
            for i in occurrence.vulnerability.package_issue:
                if i.fixed_package is not None:
                    print("Fix found.  Triggering build.")
                    buildsomething = cloudbuild_v1.CloudBuildClient()
                    buildsomething.run_build_trigger(project_id, trigger_id, {})
Ejemplo n.º 17
0
def run_build_trigger(event, context):
    # Create a client
    client = cloudbuild_v1.CloudBuildClient()

    # Initialize request argument(s)
    request = cloudbuild_v1.RunBuildTriggerRequest(
        project_id = os.getenv('project_id'),
        trigger_id = os.getenv('trigger_id'),
    )

    # Check Event structure
    print('event is {}'.format(event))
    print('context is {}'.format(context))

    # Make the request
    operation = client.run_build_trigger(request=request)

    print("Waiting for operation to complete...")

    response = operation.result()

    # Handle the response
    print(response)
    def test_cancel_build(self):
        # Setup Expected Response
        id_2 = "id23227150"
        project_id_2 = "projectId2939242356"
        status_detail = "statusDetail2089931070"
        logs_bucket = "logsBucket1565363834"
        build_trigger_id = "buildTriggerId1105559411"
        log_url = "logUrl342054388"
        expected_response = {
            "id": id_2,
            "project_id": project_id_2,
            "status_detail": status_detail,
            "logs_bucket": logs_bucket,
            "build_trigger_id": build_trigger_id,
            "log_url": log_url,
        }
        expected_response = cloudbuild_pb2.Build(**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 = cloudbuild_v1.CloudBuildClient()

        # Setup Request
        project_id = "projectId-1969970175"
        id_ = "id3355"

        response = client.cancel_build(project_id, id_)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = cloudbuild_pb2.CancelBuildRequest(
            project_id=project_id, id=id_)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Ejemplo n.º 19
0
from google.cloud.devtools import cloudbuild_v1

project_id = "xxx"
build = {
    "steps": [{
        "name": 'gcr.io/cloud-builders/gcloud',
        "args": ["config", "list"],
    }]
}
client = cloudbuild_v1.CloudBuildClient()

# v1.1.0
# response = client.create_build(project_id=project_id, build=build)

# v2.0.0
response = client.create_build(project_id, cloudbuild_v1.Build(build))