Primeros pasos con la API Service Control

En esta página se describen los pasos básicos necesarios para configurar la API Service Control en tu máquina local y probarla con el comando curl.

Configuración inicial

La API Service Control funciona con servicios gestionados. Para usar la API Service Control, primero debes crear un servicio gestionado con la API Service Management. Para obtener más información, consulta el artículo Introducción a la gestión de servicios.

Una vez que hayas creado un servicio gestionado, debes completar los siguientes pasos para usar la API Service Control desde tu servicio gestionado.

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. Install the Google Cloud CLI.

  3. Si utilizas un proveedor de identidades (IdP) externo, primero debes iniciar sesión en la CLI de gcloud con tu identidad federada.

  4. Para inicializar gcloud CLI, ejecuta el siguiente comando:

    gcloud init
  5. Create or select a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Service Control API:

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable servicecontrol.googleapis.com
  8. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/servicemanagement.serviceController

    gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE

    Replace the following:

    • PROJECT_ID: your project ID.
    • USER_IDENTIFIER: the identifier for your user account—for example, myemail@example.com.
    • ROLE: the IAM role that you grant to your user account.
  9. Install the Google Cloud CLI.

  10. Si utilizas un proveedor de identidades (IdP) externo, primero debes iniciar sesión en la CLI de gcloud con tu identidad federada.

  11. Para inicializar gcloud CLI, ejecuta el siguiente comando:

    gcloud init
  12. Create or select a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  13. Verify that billing is enabled for your Google Cloud project.

  14. Enable the Service Control API:

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable servicecontrol.googleapis.com
  15. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/servicemanagement.serviceController

    gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE

    Replace the following:

    • PROJECT_ID: your project ID.
    • USER_IDENTIFIER: the identifier for your user account—for example, myemail@example.com.
    • ROLE: the IAM role that you grant to your user account.
  16. Prueba con curl

    Primero, define un alias de shell práctico para llamar a las APIs REST de Google:

    alias gcurl='curl -H "Authorization: Bearer $(gcloud auth print-access-token)" -H "Content-Type: application/json" '
    

    La siguiente secuencia de comandos de shell muestra los pasos incrementales para llamar a la API Service Control.

    # Call with invalid service name "invalid.com". For security and privacy
    # reasons, the permission check typically happens before other checks.
    $ gcurl -d '{}' https://servicecontrol.googleapis.com/v1/services/invalid.com:check
    {
      "error": {
        "code": 403,
        "message": "Permission 'servicemanagement.services.check' denied on service 'invalid.com'.",
        "status": "PERMISSION_DENIED"
      }
    }
    
    # Call without proper permission on a service.
    $ gcurl -d '{}' https://servicecontrol.googleapis.com/v1/services/servicecontrol.googleapis.com:check
    {
      "error": {
        "code": 403,
        "message": "Permission 'servicemanagement.services.check' denied on service 'servicecontrol.googleapis.com'.",
        "status": "PERMISSION_DENIED"
      }
    }
    
    # Call with invalid request.
    $ gcurl -d '{}' https://servicecontrol.googleapis.com/v1/services/endpointsapis.appspot.com:check
    {
      "error": {
        "code": 400,
        "message": "Request contains an invalid argument.",
        "status": "INVALID_ARGUMENT"
      }
    }
    
    # This and following call assume that the service, operation name and
    # project being checked are "endpointsapis.appspot.com",
    # "google.example.hello.v1.HelloService.GetHello" and
    # "endpointsapis-consumer" correspondingly.
    # Change to the name of your managed service, operation, and project.
    # Call with invalid request.
    $ gcurl -d '{
      "operation": {
        "operationId": "123e4567-e89b-12d3-a456-426655440000",
        "consumerId": "project:endpointsapis-consumer",
        "startTime": "2016-06-12T22:00:15Z",
        "operationName": "google.example.hello.v1.HelloService.GetHello"
      }
    }' https://servicecontrol.googleapis.com/v1/services/endpointsapis.appspot.com:check
    {
      "checkErrors": [
      {
        "code": "SERVICE_NOT_ACTIVATED",
        "detail": "Service 'endpointsapis.appspot.com' is not enabled for consumer 'project:endpointsapis-consumer'."
      }
      ]
    }
    
    # Successful call to "services.check" method after the API is enabled for
    # the project.
    $ gcurl -d '{
      "operation": {
        "operationId": "123e4567-e89b-12d3-a456-426655440000",
        "consumerId": "project:endpointsapis-consumer",
        "startTime":"2016-07-31T05:20:00Z",
        "operationName":"google.example.hello.v1.HelloService.GetHello"
      }
    }' https://servicecontrol.googleapis.com/v1/services/endpointsapis.appspot.com:check
    {
      "operationId": "123e4567-e89b-12d3-a456-426655440000"
    }
    

    Una vez que hayas completado los pasos anteriores:

    • Tienes una configuración de prueba local funcional que puedes usar para llamar a cualquier API de Google Cloud Platform.
    • Tienes un servicio funcional que puedes usar con la API Service Management y la API Service Control.
    • Tienes una cuenta de servicio con los permisos correctos que puedes usar para ejecutar tu servicio.