Skip to content

Commit

Permalink
fix: add apis getChannel and deleteNetwork(#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
dayuy authored and Carrotzpc committed Mar 1, 2023
1 parent be4bf75 commit 3dcc889
Show file tree
Hide file tree
Showing 16 changed files with 128 additions and 13 deletions.
7 changes: 7 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dist
node_modules
logs
*.log
.git
static/client-sdk
public
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ ENV NODE_ENV production

COPY . /usr/src/app/

WORKDIR /usr/src/app

RUN nr build

FROM hyperledgerk8s/bc-console:dev-branch as bc-console
Expand Down
2 changes: 2 additions & 0 deletions Dockerfile.build
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ ENV NODE_ENV production

COPY . /usr/src/app/

WORKDIR /usr/src/app

RUN nr build

FROM busybox
Expand Down
21 changes: 21 additions & 0 deletions src/channel/channel.gql
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
# 通道详情
query getChannel($name: String!) {
channel(name: $name) {
name
description
creationTimestamp
epolicy {
name
description
creationTimestamp
}
members {
name
}
peers {
name
namespace
}
}
}

# 创建通道
mutation createChannel($network: String!, $channel: NewChannel!) {
channelCreate(network: $network, channel: $channel) {
Expand Down
4 changes: 3 additions & 1 deletion src/channel/channel.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Module } from '@nestjs/common';
import { forwardRef, Module } from '@nestjs/common';
import { ChannelService } from './channel.service';
import { ChannelResolver } from './channel.resolver';
import { ChannelLoader } from './channel.loader';
import { EpolicyModule } from 'src/epolicy/epolicy.module';

@Module({
providers: [ChannelService, ChannelResolver, ChannelLoader],
exports: [ChannelLoader, ChannelService],
imports: [forwardRef(() => EpolicyModule)],
})
export class ChannelModule {}
29 changes: 28 additions & 1 deletion src/channel/channel.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import {
Args,
Mutation,
Parent,
Query,
ResolveField,
Resolver,
} from '@nestjs/graphql';
import DataLoader from 'dataloader';
import { Loader } from 'src/common/dataloader';
import { Auth } from 'src/common/decorators/auth.decorator';
import { EpolicyService } from 'src/epolicy/epolicy.service';
import { Epolicy } from 'src/epolicy/models/epolicy.model';
import { Organization } from 'src/organization/models/organization.model';
import { OrganizationLoader } from 'src/organization/organization.loader';
import { JwtAuth } from 'src/types';
Expand All @@ -18,7 +21,18 @@ import { Channel } from './models/channel.model';

@Resolver(() => Channel)
export class ChannelResolver {
constructor(private readonly channelService: ChannelService) {}
constructor(
private readonly channelService: ChannelService,
private readonly epolicyService: EpolicyService,
) {}

@Query(() => Channel, { description: '通道详情' })
async channel(
@Auth() auth: JwtAuth,
@Args('name') name: string,
): Promise<Channel> {
return this.channelService.getChannel(auth, name);
}

@Mutation(() => Channel, { description: '创建通道' })
async channelCreate(
Expand Down Expand Up @@ -93,4 +107,17 @@ export class ChannelResolver {
});
return involved;
}

@ResolveField(() => [Epolicy], {
nullable: true,
description: '背书策略',
})
async epolicy(
@Auth() auth: JwtAuth,
@Parent() channel: Channel,
): Promise<Epolicy[]> {
const { name } = channel;
const epolicies = await this.epolicyService.getEpolicies(auth);
return epolicies?.filter((epolicy) => epolicy.channel === name);
}
}
8 changes: 1 addition & 7 deletions src/channel/channel.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,10 @@ export class ChannelService {
members: channel.spec?.members,
status: channel.status?.type,
peers: channel.spec?.peers,
description: channel.spec?.description,
};
}

// 权限问题,普通用户没有 list/channel
async getChannels(auth: JwtAuth): Promise<Channel[]> {
const k8s = await this.k8sService.getClient(auth);
const { body } = await k8s.channel.list();
return body.items.map((item) => this.format(item));
}

async getChannel(auth: JwtAuth, name: string): Promise<Channel> {
const k8s = await this.k8sService.getClient(auth);
const { body } = await k8s.channel.read(name);
Expand Down
7 changes: 7 additions & 0 deletions src/channel/models/channel.model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Field, ID, ObjectType } from '@nestjs/graphql';
import { SpecMember } from 'src/common/models/spec-member.model';
import { Epolicy } from 'src/epolicy/models/epolicy.model';
import { ChannelStatus } from './channel-status.enum';
import { SpecPeer } from './spec-peer.model';

Expand All @@ -8,6 +9,9 @@ export class Channel {
@Field(() => ID, { description: 'name' })
name: string;

/** 描述 */
description?: string;

/** 组织数量 */
members?: SpecMember[];

Expand All @@ -29,4 +33,7 @@ export class Channel {

/** 我参与的 */
iamInvolved?: boolean;

/** 背书策略 */
epolicy?: Epolicy[];
}
5 changes: 3 additions & 2 deletions src/epolicy/epolicy.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Module } from '@nestjs/common';
import { forwardRef, Module } from '@nestjs/common';
import { EpolicyService } from './epolicy.service';
import { EpolicyResolver } from './epolicy.resolver';
import { OrganizationModule } from 'src/organization/organization.module';
Expand All @@ -7,6 +7,7 @@ import { ChannelModule } from 'src/channel/channel.module';

@Module({
providers: [EpolicyService, EpolicyResolver],
imports: [OrganizationModule, NetworkModule, ChannelModule],
imports: [OrganizationModule, NetworkModule, forwardRef(() => ChannelModule)],
exports: [EpolicyService],
})
export class EpolicyModule {}
2 changes: 1 addition & 1 deletion src/epolicy/epolicy.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { NewEpolicyInput } from './dto/new-epolicy.input';
import { EpolicyService } from './epolicy.service';
import { Epolicy } from './models/epolicy.model';

@Resolver()
@Resolver(() => Epolicy)
export class EpolicyResolver {
constructor(private readonly epolicyService: EpolicyService) {}

Expand Down
3 changes: 3 additions & 0 deletions src/epolicy/epolicy.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export class EpolicyService {
format(epolicy: CRD.EndorsePolicy): Epolicy {
return {
name: epolicy.metadata?.name,
creationTimestamp: new Date(
epolicy.metadata?.creationTimestamp,
).toISOString(),
channel: epolicy.spec?.channel,
description: epolicy.spec?.description,
value: epolicy.spec?.value,
Expand Down
5 changes: 4 additions & 1 deletion src/epolicy/models/epolicy.model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Field, ID, ObjectType } from '@nestjs/graphql';

@ObjectType()
@ObjectType({ description: '背书策略' })
export class Epolicy {
@Field(() => ID, { description: 'name' })
name: string;
Expand All @@ -13,4 +13,7 @@ export class Epolicy {

/** 策略内容 */
value: string;

/** 创建时间 */
creationTimestamp?: string;
}
9 changes: 9 additions & 0 deletions src/network/network.gql
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,13 @@ mutation createNetwork($network: NewNetworkInput!) {
# 释放网络
mutation dissolveNetwork($name: String!, $federation: String!, $initiator: String!) {
networkDissolve(name: $name, federation: $federation, initiator: $initiator)
}

# 删除网络
mutation deleteNetwork($name: String!) {
networkDelete(name: $name) {
code
status
message
}
}
9 changes: 9 additions & 0 deletions src/network/network.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { ChannelLoader } from 'src/channel/channel.loader';
import { Channel } from 'src/channel/models/channel.model';
import { Loader } from 'src/common/dataloader';
import { Auth } from 'src/common/decorators/auth.decorator';
import { K8sV1Status } from 'src/common/models/k8s-v1-status.model';
import { NETWORK_VERSION_RESOURCES } from 'src/common/utils';
import { IbppeerService } from 'src/ibppeer/ibppeer.service';
import { Ibppeer } from 'src/ibppeer/models/ibppeer.model';
Expand Down Expand Up @@ -69,6 +70,14 @@ export class NetworkResolver {
);
}

@Mutation(() => K8sV1Status, { description: '删除网络' })
async networkDelete(
@Auth() auth: JwtAuth,
@Args('name') name: string,
): Promise<K8sV1Status> {
return this.networkService.deleteNetwork(auth, name);
}

@ResolveField(() => OrderVersion, { nullable: true, description: '配置版本' })
version(@Parent() network: Network): OrderVersion {
const { storage } = network;
Expand Down
12 changes: 12 additions & 0 deletions src/network/network.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ForbiddenException, Inject, Injectable, Logger } from '@nestjs/common';
import { ConfigType } from '@nestjs/config';
import { uniq } from 'lodash';
import { K8sV1Status } from 'src/common/models/k8s-v1-status.model';
import {
DEFAULT_INGRESS_CLASS,
DEFAULT_STORAGE_CLASS,
Expand Down Expand Up @@ -188,4 +189,15 @@ export class NetworkService {
// 3. 若投票成功,则此「释放网络」成功
return true; // 表示这个操作触发成功,而不是释放网络成功
}

async deleteNetwork(auth: JwtAuth, name: string): Promise<K8sV1Status> {
// 0. 检查是否可以删除网络
const { channelNames } = await this.getNetwork(auth, name);
if (channelNames && channelNames.length > 0) {
throw new ForbiddenException('channel also exist in the network');
}
const k8s = await this.k8sService.getClient(auth);
const { body } = await k8s.network.delete(name);
return body;
}
}
16 changes: 16 additions & 0 deletions src/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ type Channel {
"""合约数量"""
creationTimestamp: String

"""描述"""
description: String

"""背书策略"""
epolicy: [Epolicy!]

"""我参与的"""
iamInvolved: Boolean

Expand Down Expand Up @@ -45,10 +51,14 @@ enum ChannelStatus {
Error
}

"""背书策略"""
type Epolicy {
"""所在通道"""
channel: String!

"""创建时间"""
creationTimestamp: String

"""描述"""
description: String

Expand Down Expand Up @@ -218,6 +228,9 @@ type Mutation {
"""创建网络"""
networkCreate(network: NewNetworkInput!): Network!

"""删除网络"""
networkDelete(name: String!): K8sV1Status!

"""释放网络(返回true:只表示这个操作触发成功,而不是释放网络成功)"""
networkDissolve(
"""所属联盟"""
Expand Down Expand Up @@ -540,6 +553,9 @@ enum ProposalType {
}

type Query {
"""通道详情"""
channel(name: String!): Channel!

"""创建策略时,可选的通道"""
channelsForCreateEpolicy(network: String!): [Channel!]!

Expand Down

0 comments on commit 3dcc889

Please sign in to comment.