Share via

CosmosDb (Mongo API) replace offer

Anonymous
2023-06-22T23:19:26.6133333+00:00

Hello!

I'm currently working on an Azure Function written in TypeScript, and I need to implement dynamic changes to the throughput of a Cosmos DB container (MongoDb API). Specifically, I need to switch between autoscaling and manual scaling modes based on certain conditions.

I've been referring to the official documentation at this link for guidance on how to achieve this. However, I'm encountering an issue where the API call mentioned in the documentation is returning a 401 (Unauthorized) error. I have verified that the master key I'm using is correct because it works perfectly fine for retrieving all the existing offers.

My code:


  async getOfferId(databaseId: string): Promise<string> {
    const requestDateString = new Date().toUTCString();

    const auth = this.generateMasterKeyAuthorizationSignature(
      HttpMethod.GET,
      CosmosDbResourceType.offers,
      '',
      requestDateString,
    );

    const config = {
      headers: {
        'Content-Type': 'application/json',
        'x-ms-version': '2018-12-31',
        'x-ms-date': requestDateString,
        Authorization: auth,
      },
    };

    const url = `${this.baseUrl}/${CosmosDbResourceType[CosmosDbResourceType.offers]}`;

    const response = await axios.get(url, config);

    return response.data.Offers.find((offer: { offerResourceId: string }) => offer.offerResourceId === databaseId).id;
  }

  async migrateToManualscale(databaseId: string, offerId: string) {
    const resourceLink = `${CosmosDbResourceType[CosmosDbResourceType.offers]}/${offerId}`;
    const requestDateString = new Date().toUTCString();

    const auth = this.generateMasterKeyAuthorizationSignature(
      HttpMethod.PUT,
      CosmosDbResourceType.offers,
      resourceLink,
      requestDateString,
    );

    const config: AxiosRequestConfig = {
      headers: {
        'Content-Type': 'application/json',
        'x-ms-version': '2018-12-31',
        'x-ms-cosmos-migrate-offer-to-autopilot': 'true',
        'x-ms-date': requestDateString,
        Authorization: auth,
      },
    };

    try {
      const url = `${this.baseUrl}/${resourceLink}`;
      const bodyRequest = {
        offerVersion: 'V2',
        offerType: 'Invalid',
        content: {
          offerThroughput: -1,
        },
        resource: `${CosmosDbResourceType.dbs}/${databaseId}/`, // (I've tried dbs/../colls/../ too)
        offerResourceId: databaseId,
        id: offerId,
        _rid: offerId,
      };

      await axios.put(url, bodyRequest, config);
  }
  

The migrateToManualscale function does not work.

Is there any way to replace and update the offer?

I would greatly appreciate any assistance or insights you can provide to help me resolve this issue and successfully implement the dynamic throughput changes in my Azure Function.

Thank you in advance for your help!

Azure Cosmos DB
Azure Cosmos DB

An Azure NoSQL database service for app development.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.