0

I have created a cloud function to update my remote configs in a specific manner. I followed the guide as per documentation.

Following is the code snippet.

exports.updateTenantFeatures = functions
  .region(DEFAULT_LOCATION)
  .https.onCall(async (data, context) => {
    try {
     if (!context.auth) {
       throw new UnauthenticatedError(
        constants.ERROR_TEXT.CIVIS_UNAUTHENTICATED_TEXT,
        constants.ERROR_CODE.CIVIS_UNAUTHENTICATED, 
     );
    }

    const country = getCountryName(data.countryCode);
    const poolId = data.poolId;
    const remoteConfig = admin.remoteConfig();
    const template = await remoteConfig.getTemplate();

    const featureList = JSON.stringify({ [data.featureName]: data.featureState });

    template.parameterGroups[`${country}_${poolId}`].parameters[`${country}_${poolId}_features`] = {
      valueType: "JSON",
      defaultValue: {
      value: featureList,
    },
    description: `tenant of pool id ${poolId} from ${country}'s feature list`
    }

    const result = await remoteConfig.publishTemplate(template);

    return {
      result: constants.RESULT.SUCCESS, 
      tenantFeatuerList: result.parameters[`${country}_${poolId}_features`] 
    };
  
    } catch (error: any) {
      if (error?.type === constants.ERROR_TYPE.UNAUTHENTICATED ||
         error?.type ===  constants.ERROR_TYPE.UNAUTHORIZED)
      { 
        return { result: constants.RESULT.ERROR, code: error.code, details: error.message }
      }
      return { result: constants.RESULT.ERROR }
      }
   });

The goal of this function is to create parameters nested inside a parameter group programmatically. Attached image depicts how this is expected to be structured.

remote config required structure

Problem is when I'm using template.parameterGroups[`${country}_${poolId}`]... it doesn't create a parameter group as expected. It throws the following error in Logs.

error in updateTenantFeatures : TypeError: Cannot read properties of undefined (reading 'parameters')

But however if I ignore creating the parameter group and focus on creating parameters by removing the parameterGroups as template.parameters[`${country}_${poolId}_features`] = ... it creates just the parameter.

Is the parameter group creation restricted by the admin sdk ? Or is the SDK having some issue(s) in creating parameter groups ?

3
  • The error message is telling you that template.parameterGroups[`${country}_${poolId}`] is undefined. What are you expecting it to be instead? You have a lot of variables here that we can't see, so we don't know much about what you're actually working with. Try logging everything to see that it's all what you expect. Commented Jul 7, 2023 at 12:09
  • @DougStevenson I'm trying to create the parameter group along with the parameter nested inside it. I debugged this a couple of times & the issue I'm facing is that the SDK won't allow it. It seems that the SDK only allows to nest the parameter inside parameter groups that are available. Commented Jul 7, 2023 at 13:38
  • Based on this documentation it seems I can achieve my goal with the REST api. I'm attempting to do the same thing here with the node admin SDK. Commented Jul 7, 2023 at 14:18

1 Answer 1

0

I was able to figure out to do this by making a simple if condition in the code.

exports.updateTenantFeatures = functions
  .region(DEFAULT_LOCATION)
  .https.onCall(async (data, context) => {
    try {
      ...

      const country = getCountryName(data.countryCode);
      const poolId = data.poolId;
      const remoteConfig = admin.remoteConfig();
      const template = await remoteConfig.getTemplate();

      const featureList = JSON.stringify({
        [data.featureName]: data.featureState,
      });

      if (!template.parameterGroups[`${country}_${poolId}`]) {
        template.parameterGroups[`${country}_${poolId}`] = {
          parameters: {
            [`${country}_${poolId}_features`]: {
              valueType: 'JSON',
              defaultValue: {value: featureList},
            },
          },
          description: `tenant of pool id ${poolId} from ${country}'s feature list`,
        };
      } else {
        template.parameterGroups[`${country}_${poolId}`].parameters[
          `${country}_${poolId}_features`
        ] = {
          valueType: 'JSON',
          defaultValue: {
            value: featureList,
          },
          description: `tenant of pool id ${poolId} from ${country}'s feature list`,
        };
      }

      const result = await remoteConfig.publishTemplate(template);
      ...
      // Handle result
    } catch (error: any) {
      ...
      // Handle errors
    }
  });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.