Skip to content

Commit

Permalink
Merge pull request #313 from microsoft/dev
Browse files Browse the repository at this point in the history
fix deployment scope errors +deploydemo +resetdemo
  • Loading branch information
colbylwilliams authored Feb 4, 2022
2 parents 7994743 + 94e534d commit ed37889
Show file tree
Hide file tree
Showing 39 changed files with 124 additions and 205 deletions.
3 changes: 0 additions & 3 deletions src/TeamCloud.Http/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ namespace TeamCloud.Http;

public static class Extensions
{
public static HttpClient CreateHttpClient(this IHttpClientFactory factory)
=> factory.CreateHttpClient(factory.CreateMessageHandler());

public static async Task<string> ReadStringAsync(this HttpRequest request, bool leaveOpen = false)
{
if (request is null)
Expand Down
5 changes: 0 additions & 5 deletions src/TeamCloud.Http/TeamCloudHttpClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ public class TeamCloudHttpClientFactory : DefaultHttpClientFactory
public TeamCloudHttpClientFactory(TelemetryConfiguration telemetryConfiguration = null)
=> this.telemetryConfiguration = telemetryConfiguration ?? new TelemetryConfiguration(Guid.Empty.ToString());

public override HttpClient CreateHttpClient(HttpMessageHandler handler)
{
return base.CreateHttpClient(handler ?? CreateMessageHandler());
}

[SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "Lifetime is managed by the returned HttpMessageHandler instance.")]
public override HttpMessageHandler CreateMessageHandler()
{
Expand Down
9 changes: 3 additions & 6 deletions src/TeamCloud.Http/TeamCloudHttpMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,9 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
{
Debug.WriteLine($"<=> {request.Method.ToString().ToUpperInvariant()} {request.RequestUri}");

response = (await request.RequestUri.ToString()
.AllowAnyHttpStatus()
.WithHeaders(request.Headers)
.SendAsync(request.Method, request.Content, cancellationToken)
.ConfigureAwait(false))
.ResponseMessage;
response = await base
.SendAsync(request, cancellationToken)
.ConfigureAwait(false);
}

return response;
Expand Down
13 changes: 11 additions & 2 deletions web/src/API.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.


import { Auth } from './Auth';
import { Project, TeamCloud } from 'teamcloud';
import { ErrorResult, Project, TeamCloud } from 'teamcloud';
import { HubConnection, HubConnectionBuilder, IHttpConnectionOptions } from '@microsoft/signalr'

const _getApiUrl = () => {
Expand All @@ -19,12 +18,22 @@ const _getApiUrl = () => {
export const apiUrl = _getApiUrl();

export const auth = new Auth();
// export const api = new TeamCloud(auth, apiUrl, { credentialScopes: [], retryOptions: { maxRetries: 0 } });
export const api = new TeamCloud(auth, apiUrl, { credentialScopes: [] });

const httpOptions: IHttpConnectionOptions = {
accessTokenFactory: async () => (await auth.getToken())?.token ?? '',
}

export const onResponse = (raw: any, flat: unknown) => {
if (raw.status >= 400) {
const error = flat as ErrorResult
if (error)
throw error
throw new Error(raw.parsedBody || raw.bodyAsText || `Error: ${raw.status}`)
}
}

let connection: HubConnection | undefined

export const resolveSignalR = async (project: Project | undefined, callback: (action: string, data: any) => void) => {
Expand Down
2 changes: 1 addition & 1 deletion web/src/components/common/ContentProgress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ export const ContentProgress: React.FC<IContentProgressProps> = (props) => {
<ProgressIndicator
percentComplete={props.percentComplete}
progressHidden={props.progressHidden}
styles={{ itemProgress: { padding: '0px', marginTop: '-2px' } }} />
styles={{ itemProgress: { padding: '0px', /*marginTop: '-2px' */ } }} />
);
}
6 changes: 4 additions & 2 deletions web/src/hooks/useAdapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@

import { useQuery } from 'react-query'
import { useIsAuthenticated } from '@azure/msal-react';
import { api } from '../API';
import { api, onResponse } from '../API';

export const useAdapters = () => {

const isAuthenticated = useIsAuthenticated();

return useQuery(['adapters'], async () => {
const { data } = await api.getAdapters();
const { data } = await api.getAdapters({
onResponse: onResponse
});
return data;
}, {
enabled: isAuthenticated
Expand Down
7 changes: 2 additions & 5 deletions web/src/hooks/useAuditCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { useQuery } from 'react-query'
import { useIsAuthenticated } from '@azure/msal-react';
import { api } from '../API';
import { api, onResponse } from '../API';
import { useOrg } from '.';

export const useAuditCommands = () => {
Expand All @@ -15,10 +15,7 @@ export const useAuditCommands = () => {
return useQuery(['org', org?.id, 'audit', 'commands'], async () => {

const { data } = await api.getAuditCommands(org!.id, {
onResponse: (raw, flat) => {
if (raw.status >= 400)
throw new Error(raw.parsedBody || raw.bodyAsText || `Error: ${raw.status}`)
}
onResponse: onResponse
});

return data;
Expand Down
9 changes: 3 additions & 6 deletions web/src/hooks/useAuthorizeDeploymentScope.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import { useQueryClient, useMutation } from "react-query";
import { DeploymentScope } from "teamcloud";
import { useDeploymentScopes } from ".";
import { api } from "../API";
import { api, onResponse } from "../API";



export const useAuthorizeDeployemntScope = () => {

const { data: deploymentScopes } = useDeploymentScopes();
const { data: deploymentScopes } = useDeploymentScopes();

const queryClient = useQueryClient();

return useMutation(async (deploymentScope: DeploymentScope) => {

const { data } = await api.initializeAuthorization(deploymentScope.organization, deploymentScope.id, {
onResponse: (raw, flat) => {
if (raw.status >= 400)
throw new Error(raw.parsedBody || raw.bodyAsText || `Error: ${raw.status}`)
}
onResponse: onResponse
});

return data;
Expand Down
12 changes: 5 additions & 7 deletions web/src/hooks/useCancelProjectComponentTask.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { useQueryClient, useMutation } from "react-query";
import { useNavigate, useParams } from "react-router-dom";
import { ComponentTask } from "teamcloud";
import { useOrg, useProject, useProjectComponent, useProjectComponentTasks } from ".";
import { api } from "../API";


import { api, onResponse } from "../API";

export const useCancelProjectComponentTask = () => {

Expand All @@ -22,10 +23,7 @@ export const useCancelProjectComponentTask = () => {
return useMutation(async (componentTask: ComponentTask) => {

const { data } = await api.cancelComponentTask(componentTask?.organization, componentTask.projectId, componentTask.componentId, componentTask.id, {
onResponse: (raw, flat) => {
if (raw.status >= 400)
throw new Error(raw.parsedBody || raw.bodyAsText || `Error: ${raw.status}`)
}
onResponse: onResponse
});

return data;
Expand Down
7 changes: 2 additions & 5 deletions web/src/hooks/useCreateDeploymentScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { useMutation, useQueryClient } from 'react-query'
import { DeploymentScopeDefinition } from 'teamcloud';
import { api } from '../API';
import { api, onResponse } from '../API';
import { useOrg } from '.';

export const useCreateDeploymentScope = () => {
Expand All @@ -17,10 +17,7 @@ export const useCreateDeploymentScope = () => {

const { data } = await api.createDeploymentScope(org.id, {
body: scopeDef,
onResponse: (raw, flat) => {
if (raw.status >= 400)
throw new Error(raw.parsedBody || raw.bodyAsText || `Error: ${raw.status}`)
},
onResponse: onResponse
});

return data;
Expand Down
35 changes: 13 additions & 22 deletions web/src/hooks/useCreateOrg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { useNavigate } from 'react-router-dom';
import { useMutation, useQueryClient } from 'react-query'
import { DeploymentScopeDefinition, OrganizationDefinition, ProjectTemplateDefinition } from 'teamcloud';
import { api } from '../API';
import { api, onResponse } from '../API';

export const useCreateOrg = () => {

Expand All @@ -13,43 +13,34 @@ export const useCreateOrg = () => {

return useMutation(async (def: { orgDef: OrganizationDefinition, scopeDef?: DeploymentScopeDefinition, templateDef?: ProjectTemplateDefinition }) => {

console.log(`- createOrg`);
console.debug(`- createOrg`);
const orgResponse = await api.createOrganization({
body: def.orgDef,
onResponse: (raw, flat) => {
if (raw.status >= 400)
throw new Error(raw.parsedBody || raw.bodyAsText || `Error: ${raw.status}`)
}
onResponse: onResponse
});
const newOrg = orgResponse.data;
console.log(`+ createOrg`);
console.debug(`+ createOrg`);

let scope, template;

if (newOrg?.id) {
if (def.scopeDef) {
console.log(`- createDeploymentScope`);
console.debug(`- createDeploymentScope`);
const scopeResponse = await api.createDeploymentScope(newOrg.id, {
body: def.scopeDef,
onResponse: (raw, flat) => {
if (raw.status >= 400)
throw new Error(raw.parsedBody || raw.bodyAsText || `Error: ${raw.status}`)
}
onResponse: onResponse
});
scope = scopeResponse.data;
console.log(`+ createDeploymentScope`);
console.debug(`+ createDeploymentScope`);
}
if (def.templateDef) {
console.log(`- createProjectTemplate`);
console.debug(`- createProjectTemplate`);
const templateResponse = await api.createProjectTemplate(newOrg.id, {
body: def.templateDef,
onResponse: (raw, flat) => {
if (raw.status >= 400)
throw new Error(raw.parsedBody || raw.bodyAsText || `Error: ${raw.status}`)
}
onResponse: onResponse
});
template = templateResponse.data;
console.log(`+ createProjectTemplate`);
console.debug(`+ createProjectTemplate`);
}
}

Expand All @@ -62,16 +53,16 @@ export const useCreateOrg = () => {
queryClient.invalidateQueries('orgs');

if (data.scope) {
console.log(`+ setDeploymentScopes (${data.org.slug})`);
console.debug(`+ setDeploymentScopes (${data.org.slug})`);
queryClient.setQueryData(['org', data.org.id, 'scopes'], [data.scope])
}

if (data.template) {
console.log(`+ setProjectTemplates (${data.org.slug})`);
console.debug(`+ setProjectTemplates (${data.org.slug})`);
queryClient.setQueryData(['org', data.org.id, 'templates'], [data.template])
}

console.log(`+ setOrg (${data.org.slug})`);
console.debug(`+ setOrg (${data.org.slug})`);
queryClient.setQueryData(['org', data.org.slug], data.org)

navigate(`/orgs/${data.org.slug}`);
Expand Down
9 changes: 3 additions & 6 deletions web/src/hooks/useCreateProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { useMutation, useQueryClient } from 'react-query'
import { useNavigate } from 'react-router-dom';
import { ProjectDefinition } from 'teamcloud';
import { api } from '../API';
import { api, onResponse } from '../API';
import { useOrg, useProjects } from '.';

export const useCreateProject = () => {
Expand All @@ -21,10 +21,7 @@ export const useCreateProject = () => {

const { data } = await api.createProject(org.id, {
body: projectDef,
onResponse: (raw, flat) => {
if (raw.status >= 400)
throw new Error(raw.parsedBody || raw.bodyAsText || `Error: ${raw.status}`)
}
onResponse: onResponse
});

return data;
Expand All @@ -36,6 +33,6 @@ export const useCreateProject = () => {

navigate(`/orgs/${org.slug}/projects/${data.slug}`);
}
}
},
}).mutateAsync
}
7 changes: 2 additions & 5 deletions web/src/hooks/useCreateProjectComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { useMutation, useQueryClient } from 'react-query'
import { useNavigate, useParams } from 'react-router-dom';
import { ComponentDefinition } from 'teamcloud';
import { api } from '../API';
import { api, onResponse } from '../API';
import { useOrg, useProject, useProjectComponents } from '.';

export const useCreateProjectComponent = () => {
Expand All @@ -24,10 +24,7 @@ export const useCreateProjectComponent = () => {

const { data } = await api.createComponent(project.organization, project.id, {
body: componentDef,
onResponse: (raw, flat) => {
if (raw.status >= 400)
throw new Error(raw.parsedBody || raw.bodyAsText || `Error: ${raw.status}`)
}
onResponse: onResponse
});

return data;
Expand Down
7 changes: 2 additions & 5 deletions web/src/hooks/useCreateProjectComponentTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { useMutation, useQueryClient } from 'react-query'
import { useNavigate, useParams } from 'react-router-dom';
import { ComponentTaskDefinition } from 'teamcloud';
import { api } from '../API';
import { api, onResponse } from '../API';
import { useOrg, useProject, useProjectComponent, useProjectComponentTasks } from '.';

export const useCreateProjectComponentTask = () => {
Expand All @@ -26,10 +26,7 @@ export const useCreateProjectComponentTask = () => {

const { data } = await api.createComponentTask(project.organization, project.id, component.id, {
body: componentTaskDef,
onResponse: (raw, flat) => {
if (raw.status >= 400)
throw new Error(raw.parsedBody || raw.bodyAsText || `Error: ${raw.status}`)
}
onResponse: onResponse
});

return data;
Expand Down
7 changes: 2 additions & 5 deletions web/src/hooks/useCreateProjectSchedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { useMutation, useQueryClient } from 'react-query'
import { useNavigate, useParams } from 'react-router-dom';
import { ScheduleDefinition } from 'teamcloud';
import { api } from '../API';
import { api, onResponse } from '../API';
import { useOrg, useProject, useProjectSchedules } from '.';

export const useCreateProjectSchedule = () => {
Expand All @@ -24,10 +24,7 @@ export const useCreateProjectSchedule = () => {

const { data } = await api.createSchedule(project.organization, project.id, {
body: scheduletDef,
onResponse: (raw, flat) => {
if (raw.status >= 400)
throw new Error(raw.parsedBody || raw.bodyAsText || `Error: ${raw.status}`)
}
onResponse: onResponse
});

return data;
Expand Down
12 changes: 3 additions & 9 deletions web/src/hooks/useCreateProjectTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { useMutation, useQueryClient } from 'react-query'
import { ProjectTemplateDefinition } from 'teamcloud';
import { api } from '../API';
import { api, onResponse } from '../API';
import { useOrg } from '.';

export const useCreateProjectTemplate = () => {
Expand All @@ -17,18 +17,12 @@ export const useCreateProjectTemplate = () => {

const { data } = await api.createProjectTemplate(org.id, {
body: templateDef,
onResponse: (raw, flat) => {
// console.warn(JSON.stringify(raw))
// console.warn(JSON.stringify(flat))
if (raw.status >= 400)
throw new Error(raw.parsedBody || raw.bodyAsText || `Error: ${raw.status}`)
}
onResponse: onResponse
});
console.warn(JSON.stringify(data))

return data;
}, {
onSuccess: data => {
console.warn('onsuccess')
if (data) {
queryClient.invalidateQueries(['org', org!.id, 'templates'])
queryClient.setQueryData(['org', org!.id, 'templates', data!.id], data);
Expand Down
Loading

0 comments on commit ed37889

Please sign in to comment.