Search code examples
google-cloud-platformgoogle-compute-enginegoogle-apis-explorer

GCP API - machineImages.insert returns 503


In my application, I am creating images from machines for later reuse and instance creation purposes.

I have the following JAVA code to create the images:

public static String createGCPImage(String imageName, String projectId, String machineName) throws Exception {
        String machineNameUrl = "projects/" + projectId + "/global/instances/" + machineName;
        JsonObject imageRequest = new JsonObject();
        imageRequest.addProperty("name", imageName);
        imageRequest.addProperty("sourceInstance", machineNameUrl);
        String url = "https://compute.googleapis.com/compute/beta/projects/" + projectId + "/global/machineImages";
        
        ProjectOperation po = new ProjectOperation();
        po.setProjectId(order.getProject()).setServiceAccount();
        String serviceAccount = po.getServiceAccount();
        InputStream credentialStream = new ByteArrayInputStream(serviceAccount.getBytes());
        GoogleCredentials credentials = GoogleCredentials.fromStream(credentialStream);
        List<String> COMPUTE_SCOPES = Collections.singletonList(ComputeScopes.COMPUTE);
        if (credentials.createScopedRequired()) {
            credentials = credentials.createScoped(COMPUTE_SCOPES);
        }
        
        Gson gson = new Gson();
        String requestBody = gson.toJson(imageRequest);
        final HttpContent content = new ByteArrayContent("application/json", requestBody.getBytes("UTF8") );
        HttpRequestFactory factory = new NetHttpTransport().createRequestFactory(new HttpCredentialsAdapter(credentials));
        HttpRequest request = factory.buildPostRequest(new GenericUrl(url), content);
        HttpResponse httpResponse = request.execute();

        return httpResponse.getStatusMessage();
    }


This request returns the following response:

Error: 503 Service Unavailable
POST https://compute.googleapis.com/compute/beta/projects/devopsucp/global/machineImages
{
  "error": {
    "code": 503,
    "message": "Internal error. Please try again or contact Google Support. (Code: '5B7AFB2001A53.AD5AC0C.4B01291B')",
    "errors": [
      {
        "message": "Internal error. Please try again or contact Google Support. (Code: '5B7AFB2001A53.AD5AC0C.4B01291B')",
        "domain": "global",
        "reason": "backendError"
      }
    ]
  }
}

When I try the same request from the GCP APIs explorer, I get the same error:

{
  "error": {
    "code": 503,
    "message": "Internal error. Please try again or contact Google Support. (Code: '5B7B0120B5674.A1CBB85.7900FC0D')",
    "errors": [
      {
        "message": "Internal error. Please try again or contact Google Support. (Code: '5B7B0120B5674.A1CBB85.7900FC0D')",
        "domain": "global",
        "reason": "backendError"
      }
    ]
  }
}

The body of the request is as follows:

{
  "name": "test-machine-image-name",
  "sourceInstance": "https://www.googleapis.com/compute/v1/projects/devopsucp/global/instances/urigcpdev1"
}

Solution

  • I have found my error! In the request body I specified sourceInstance as:

    "https://www.googleapis.com/compute/v1/projects/devopsucp/global/instances/urigcpdev1"
    

    which should have been:

    "https://www.googleapis.com/compute/v1/projects/devopsucp/zones/{machineZone}/instances/urigcpdev1"
    

    Once I fix the instanceName parameter, I receive a 200 response and the image is created as expected :).

    The 503 response I was getting before is quite confusing...