shamrickus commented on code in PR #7497:
URL: https://github.com/apache/trafficcontrol/pull/7497#discussion_r1188717645


##########
experimental/traffic-portal/nightwatch/dataClient.ts:
##########
@@ -0,0 +1,419 @@
+/*
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import * as https from "https";
+
+import axios, { AxiosError, AxiosInstance } from "axios";
+import { CreatedData } from "nightwatch/globals/globals";
+import {
+       CDN,
+       GeoLimit,
+       GeoProvider,
+       LoginRequest,
+       ProfileType,
+       Protocol,
+       RequestASN,
+       RequestCacheGroup,
+       RequestCoordinate,
+       RequestDeliveryService,
+       RequestDivision,
+       RequestPhysicalLocation,
+       RequestProfile,
+       RequestRegion, RequestServer, RequestStatus,
+       RequestSteeringTarget,
+       RequestTenant,
+       RequestType,
+       ResponseCacheGroup,
+       ResponseDeliveryService,
+       ResponseDivision,
+       ResponsePhysicalLocation, ResponseProfile,
+       ResponseRegion, ResponseStatus,
+       TypeFromResponse
+} from "trafficops-types";
+
+/**
+ * Defines the class used to create test data for the E2E environment
+ */
+export class DataClient {
+       private readonly toURL: string;
+       private readonly apiVersion: string;
+       private readonly adminUser: string;
+       private readonly adminPass: string;
+       /** Tracks if the client has logged in */
+       public loggedIn = false;
+       /** Client used to talk to the TO API */
+       private readonly client: AxiosInstance;
+       public constructor(toURL: string, apiVersion: string, adminUser: 
string, adminPass: string) {
+               this.toURL = toURL;
+               this.apiVersion = apiVersion;
+               this.adminUser = adminUser;
+               this.adminPass = adminPass;
+
+               this.client = axios.create({
+                       httpsAgent: new https.Agent({
+                               rejectUnauthorized: false
+                       })
+               });
+       }
+
+       /**
+        * Creates data needed for the E2E tests
+        *
+        * @param id ID added to various fields to ensure that creation occurs 
regardless of environment
+        */
+       public async createData(id: string): Promise<CreatedData> {
+               const apiUrl = `${this.toURL}/api/${this.apiVersion}`;
+               if 
(Object.keys(this.client.defaults.headers.common).indexOf("Cookie") === -1) {
+                       this.loggedIn = false;
+                       let accessToken = "";
+                       const loginReq: LoginRequest = {
+                               p: this.adminPass,
+                               u: this.adminUser
+                       };
+                       try {
+                               const logResp = await 
this.client.post(`${apiUrl}/user/login`, JSON.stringify(loginReq));
+                               if (logResp.headers["set-cookie"]) {
+                                       for (const cookie of 
logResp.headers["set-cookie"]) {
+                                               if 
(cookie.indexOf("access_token") > -1) {
+                                                       accessToken = cookie;
+                                                       break;
+                                               }
+                                       }
+                               }
+                       } catch (e) {
+                               console.error((e as AxiosError).message);
+                               throw e;
+                       }
+                       if (accessToken === "") {
+                               const e = new Error("Access token is not set");
+                               console.error(e.message);
+                               throw e;
+                       }
+                       this.loggedIn = true;
+                       this.client.defaults.headers.common = {Cookie: 
accessToken};
+               }
+
+               const cdn: CDN = {
+                       dnssecEnabled: false, domainName: `tests${id}.com`, 
name: `testCDN${id}`
+               };
+
+               let resp = await this.client.get(`${apiUrl}/types`);
+               const types: Array<TypeFromResponse> = resp.data.response;
+               const httpType = types.find(typ => typ.name === "HTTP" && 
typ.useInTable === "deliveryservice");
+               if (httpType === undefined) {
+                       throw new Error("Unable to find `HTTP` type");
+               }
+               const steeringType = types.find(typ => typ.name === "STEERING" 
&& typ.useInTable === "deliveryservice");
+               if (steeringType === undefined) {
+                       throw new Error("Unable to find `STEERING` type");
+               }
+               const steeringWeightType = types.find(typ => typ.name === 
"STEERING_WEIGHT" && typ.useInTable === "steering_target");
+               if (steeringWeightType === undefined) {
+                       throw new Error("Unable to find `STEERING_WEIGHT` 
type");
+               }
+               const cgType = types.find(typ => typ.useInTable === 
"cachegroup");
+               if (!cgType) {
+                       throw new Error("Unable to find any Cache Group Types");
+               }
+               const edgeType = types.find(typ => typ.useInTable === "server" 
&& typ.name === "EDGE");
+               if (edgeType === undefined) {
+                       throw new Error("Unable to find `EDGE` type");
+               }
+
+               const data = {} as CreatedData;
+               let url = `${apiUrl}/cdns`;
+               try {
+                       resp = await this.client.post(url, JSON.stringify(cdn));
+               } catch(e) {
+                       (e as AxiosError).message += ` Failed on CDN post as 
${url}`;
+                       throw e;
+               }
+               const respCDN = resp.data.response;
+               data.cdn = respCDN;
+
+               const ds: RequestDeliveryService = {
+                       active: false,
+                       cacheurl: null,
+                       cdnId: respCDN.id,
+                       displayName: `test DS${id}`,
+                       dscp: 0,
+                       ecsEnabled: false,
+                       edgeHeaderRewrite: null,
+                       fqPacingRate: null,
+                       geoLimit: GeoLimit.NONE,
+                       geoProvider: GeoProvider.MAX_MIND,
+                       httpBypassFqdn: null,
+                       infoUrl: null,
+                       initialDispersion: 1,
+                       ipv6RoutingEnabled: false,
+                       logsEnabled: false,
+                       maxOriginConnections: 0,
+                       maxRequestHeaderBytes: 0,
+                       midHeaderRewrite: null,
+                       missLat: 0,
+                       missLong: 0,
+                       multiSiteOrigin: false,
+                       orgServerFqdn: "http://test.com";,
+                       profileId: 1,
+                       protocol: Protocol.HTTP,
+                       qstringIgnore: 0,
+                       rangeRequestHandling: 0,
+                       regionalGeoBlocking: false,
+                       remapText: null,
+                       routingName: "test",
+                       signed: false,
+                       tenantId: 1,
+                       typeId: httpType.id,
+                       xmlId: `testDS${id}`
+               };
+               url = `${apiUrl}/deliveryservices`;
+               try {
+                       resp = await this.client.post(url, JSON.stringify(ds));
+               } catch(e) {
+                       (e as AxiosError).message += ` Failed on DS post at 
${url}`;
+                       throw e;
+               }

Review Comment:
   You are correct, I had it doing more stuff but all of that info is already 
on the error.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to