public static ProductBundle ParseProductBundle(dynamic productBundleInfo) { ProductBundle productBundle = new ProductBundle(); try { /// "productBundle": { /// "id": "19", /// "name": "Large Windows Server 2012 R2", /// "description": "This is a Large Windows Server 2012 R2 instance (2 CPU @1GHz, 2GB RAM) charged at monthly rate.", /// "code": "CCPDixonLargeWindowsServer2012R2" /// }, dynamic prodBundleJson = productBundleInfo.productBundle; productBundle.Name = (string)prodBundleJson.name; productBundle.Description = (string)prodBundleJson.description; productBundle.Code = (string)prodBundleJson.code; productBundle.Id = (string)prodBundleJson.id; productBundle.ServiceInstanceName = (string)prodBundleJson.serviceInstanceId.name; } catch (RuntimeBinderException e) { String errMsg = "Malformed productBundle object" + productBundle.ToString(); logger.Error(errMsg, e); throw; } return(productBundle); }
/// <summary> /// Extract charges. This implementation assumes there is one non-recuring and one recurring charge, /// and that hte non-recurring occurs first. /// </summary> /// <param name="productBundle"></param> /// <param name="productBundleInfo"></param> public static void ParseBundleRates(ProductBundle productBundle, dynamic productBundleInfo) { productBundle.RateCardCharges = new List <string>(); try { foreach (dynamic rateCardCharge in productBundleInfo.rateCardCharges) { /// "rateCardCharges": [ /// { /// "price": "5.00" /// }, /// { /// "price": "0.00" /// } /// ], productBundle.RateCardCharges.Add((string)rateCardCharge.price); productBundle.ChargeFrequency = (string)rateCardCharge.rateCardComponent.rateCard.chargeType.displayName; } } catch (RuntimeBinderException e) { String errMsg = "Malformed productBundle object" + productBundleInfo.ToString(); logger.Error(errMsg, e); throw; } }
public static List <ProductBundle> ParseJson(dynamic result) { List <ProductBundle> bundles = new List <ProductBundle>(); string jsonSerialisedResult = result.ToString(); logger.Debug("CPBM response " + jsonSerialisedResult); // Don't use Linq until you need to select specific objects in a colletion based on a specific condition. // Exception handling v checking property exists examined here: // http://stackoverflow.com/a/20001358/939250 // dynamic catalog; try { catalog = result.catalog; } catch (RuntimeBinderException e) { String errMsg = "Missing catalog object in " + jsonSerialisedResult; logger.Error(errMsg, e); throw; } JArray productBundleRevisions; try { productBundleRevisions = catalog.productBundleRevisions; } catch (RuntimeBinderException e) { String errMsg = "Missing productBundleRevisions object in " + jsonSerialisedResult; logger.Error(errMsg, e); throw; } foreach (dynamic productBundleInfo in productBundleRevisions) { ProductBundle productBundle = ParseProductBundle(productBundleInfo); // Screen out service instances by name if (productBundle.ServiceInstanceName != DT2.Properties.Settings.Default.CPBMServiceInstanceName) { logger.Debug("Ignoring product bundle " + productBundle.Name + " due to service instance, which is " + productBundle.ServiceInstanceName); continue; } ParseBundleConstraint(productBundleInfo, productBundle); ParseBundleRates(productBundle, productBundleInfo); logger.Debug("Added ProductBundle code" + productBundle.Code); bundles.Add(productBundle); } return(bundles); }
public static Subscription ParseSubscriptionJson(dynamic newSubJson) { Subscription newSub = new Subscription(); string tmp = newSubJson.ToString(); newSub.Uuid = new Guid((String)(newSubJson.uuid)); newSub.State = (String)(newSubJson.state); newSub.Product = new ProductBundle(); newSub.HostName = (string)newSubJson.configurationData.hostName; try { newSub.Product = ProductBundle.ParseProductBundle(newSubJson); } catch (RuntimeBinderException e) { String errMsg = "No product bundle for subscription " + newSub.Uuid; logger.Debug(errMsg, e); } return(newSub); }
public static void ParseBundleConstraint(dynamic productBundleInfo, ProductBundle productBundle) { try { productBundle.ServiceOfferingUuid = new List <string>(); productBundle.ImageTemplateUuid = new List <string>(); foreach (dynamic constraint in productBundleInfo.provisioningConstraints) { // "provisioningConstraints": [ // { // "association": "INCLUDES", // "componentName": "serviceOfferingUuid", // "value": "f1a234ae-1d5a-4fbc-817f-40c0c65e4e32" // } //] string contraintType = (string)constraint.componentName; if (contraintType == "serviceOfferingUuid") { string constraintUuid = (string)constraint.value; logger.Debug("Adding ServiceOfferingUuid constraint" + constraintUuid); productBundle.ServiceOfferingUuid.Add(constraintUuid); } else if (contraintType == "templateUuid") { string constraintUuid = (string)constraint.value; logger.Debug("Adding templateUuid constraint" + constraintUuid); productBundle.ImageTemplateUuid.Add(constraintUuid); } } } catch (RuntimeBinderException e) { String errMsg = "ProductBundle object with no constraints" + productBundleInfo.ToString(); logger.Error(errMsg, e); } }