public async Task UpdateVariableGroup()

in Webapp/SDAF/Controllers/RestHelper.cs [399:446]


        public async Task UpdateVariableGroup(EnvironmentModel environment, string newName, string description)
        {
            string uri = $"{collectionUri}{project}/_apis/distributedtask/variablegroups/{environment.id}?api-version=7.1";

            // Get the existing environment
            using HttpResponseMessage getResponse = client.GetAsync(uri).Result;
            string getResponseBody = await getResponse.Content.ReadAsStringAsync();
            HandleResponse(getResponse, getResponseBody);

            EnvironmentModel existingEnvironment = JsonSerializer.Deserialize<EnvironmentModel>(getResponseBody);

            // Persist and update the project reference
            environment.variableGroupProjectReferences = existingEnvironment.variableGroupProjectReferences;
            if (environment.variableGroupProjectReferences != null && environment.variableGroupProjectReferences.Length > 0)
            {
                newName = "SDAF-" + newName.Replace("SDAF-", "");
                environment.variableGroupProjectReferences[0].name = newName;
                environment.variableGroupProjectReferences[0].description = description;
            }
            else
            {
                throw new Exception("Existing environment project reference was empty");
            }

            // Persist any existing variables
            string environmentJsonString = JsonConvert.SerializeObject(environment);
            string variablesJsonString = JsonDocument.Parse(getResponseBody).RootElement.GetProperty("variables").ToString();
            dynamic dynamicEnvironment = JsonConvert.DeserializeObject(environmentJsonString);
            dynamic dynamicVariables = JsonConvert.DeserializeObject(variablesJsonString);

            dynamicVariables.Agent = JToken.FromObject(environment.variables.Agent);
            dynamicVariables.ARM_CLIENT_ID = JToken.FromObject(environment.variables.ARM_CLIENT_ID);
            dynamicVariables.ARM_CLIENT_SECRET = JToken.FromObject(environment.variables.ARM_CLIENT_SECRET);
            dynamicVariables.ARM_TENANT_ID = JToken.FromObject(environment.variables.ARM_TENANT_ID);
            dynamicVariables.ARM_SUBSCRIPTION_ID = JToken.FromObject(environment.variables.ARM_SUBSCRIPTION_ID);
            dynamicVariables.sap_fqdn = JToken.FromObject(environment.variables.sap_fqdn);
            dynamicVariables.POOL = JToken.FromObject(environment.variables.POOL);

            dynamicEnvironment.variables = dynamicVariables;

            // Make the put call
            string requestJson = JsonConvert.SerializeObject(dynamicEnvironment, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
            StringContent content = new(requestJson, Encoding.ASCII, "application/json");

            HttpResponseMessage putResponse = await client.PutAsync(uri, content);
            string putResponseBody = await putResponse.Content.ReadAsStringAsync();
            HandleResponse(putResponse, putResponseBody);
        }