Skip to content

Commit

Permalink
mpg, add host parameter if server is synthesized by `@armProviderNa…
Browse files Browse the repository at this point in the history
…mespace` (#2532)

* add host parameter for arm

* re-generate test code

* format, comment

* lint

---------

Co-authored-by: actions-user <action@github.com>
  • Loading branch information
XiaofeiCao and actions-user committed Jan 26, 2024
1 parent 95268e1 commit 28d35e5
Show file tree
Hide file tree
Showing 8 changed files with 294 additions and 94 deletions.
20 changes: 16 additions & 4 deletions typespec-extension/src/code-model-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ export class CodeModelBuilder {

private processHost(server: HttpServer | undefined): Parameter[] {
const hostParameters: Parameter[] = [];
if (server) {
if (server && !this.isArmSynthesizedServer(server)) {
server.parameters.forEach((it) => {
let parameter;

Expand Down Expand Up @@ -542,7 +542,7 @@ export class CodeModelBuilder {
// server
let baseUri = "{endpoint}";
const servers = getServers(this.program, client.service);
if (servers && servers.length === 1) {
if (servers && servers.length === 1 && !this.isArmSynthesizedServer(servers[0])) {
baseUri = servers[0].url;
}
const hostParameters = this.processHost(servers?.length === 1 ? servers[0] : undefined);
Expand Down Expand Up @@ -629,6 +629,18 @@ export class CodeModelBuilder {
return clients;
}

/**
* `@armProviderNamespace` currently will add a default server if not defined globally:
* https://github.com/Azure/typespec-azure/blob/8b8d7c05f168d9305a09691c4fedcb88f4a57652/packages/typespec-azure-resource-manager/src/namespace.ts#L121-L128
* TODO: if the synthesized server has the right hostParameter, we can use that insteadß
*
* @param server returned by getServers
* @returns whether it's synthesized by `@armProviderNamespace`
*/
private isArmSynthesizedServer(server: HttpServer): boolean {
return this.isArm() && (!server.parameters || server.parameters.size == 0);
}

private needToSkipProcessingOperation(operation: Operation, clientContext: ClientContext): boolean {
// don't generate protocol and convenience method for overloaded operations
// issue link: https://github.com/Azure/autorest.java/issues/1958#issuecomment-1562558219 we will support generate overload methods for non-union type in future (TODO issue: https://github.com/Azure/autorest.java/issues/2160)
Expand Down Expand Up @@ -2778,8 +2790,8 @@ export class CodeModelBuilder {
}
}

private isArm() {
return this.codeModel.arm;
private isArm(): boolean {
return Boolean(this.codeModel.arm);
}

private isSchemaUsageEmpty(schema: Schema): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ private ArmResourceProviderManager(HttpPipeline httpPipeline, AzureProfile profi
Objects.requireNonNull(httpPipeline, "'httpPipeline' cannot be null.");
Objects.requireNonNull(profile, "'profile' cannot be null.");
this.clientObject = new ArmResourceProviderClientBuilder().pipeline(httpPipeline)
.subscriptionId(profile.getSubscriptionId()).defaultPollInterval(defaultPollInterval).buildClient();
.endpoint(profile.getEnvironment().getResourceManagerEndpoint()).subscriptionId(profile.getSubscriptionId())
.defaultPollInterval(defaultPollInterval).buildClient();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
* The interface for ArmResourceProviderClient class.
*/
public interface ArmResourceProviderClient {
/**
* Gets Server parameter.
*
* @return the endpoint value.
*/
String getEndpoint();

/**
* Gets Version parameter.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@
*/
@ServiceClientBuilder(serviceClients = { ArmResourceProviderClientImpl.class })
public final class ArmResourceProviderClientBuilder {
/*
* Server parameter
*/
private String endpoint;

/**
* Sets Server parameter.
*
* @param endpoint the endpoint value.
* @return the ArmResourceProviderClientBuilder.
*/
public ArmResourceProviderClientBuilder endpoint(String endpoint) {
this.endpoint = endpoint;
return this;
}

/*
* The ID of the target subscription.
*/
Expand Down Expand Up @@ -113,7 +129,7 @@ public ArmResourceProviderClientImpl buildClient() {
SerializerAdapter localSerializerAdapter = (serializerAdapter != null) ? serializerAdapter
: SerializerFactory.createDefaultManagementSerializerAdapter();
ArmResourceProviderClientImpl client = new ArmResourceProviderClientImpl(localPipeline, localSerializerAdapter,
localDefaultPollInterval, localEnvironment, this.subscriptionId);
localDefaultPollInterval, localEnvironment, this.endpoint, this.subscriptionId);
return client;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@
*/
@ServiceClient(builder = ArmResourceProviderClientBuilder.class)
public final class ArmResourceProviderClientImpl implements ArmResourceProviderClient {
/**
* Server parameter.
*/
private final String endpoint;

/**
* Gets Server parameter.
*
* @return the endpoint value.
*/
public String getEndpoint() {
return this.endpoint;
}

/**
* Version parameter.
*/
Expand Down Expand Up @@ -159,13 +173,15 @@ public OperationsClient getOperations() {
* @param serializerAdapter The serializer to serialize an object into a string.
* @param defaultPollInterval The default poll interval for long-running operation.
* @param environment The Azure environment.
* @param endpoint Server parameter.
* @param subscriptionId The ID of the target subscription.
*/
ArmResourceProviderClientImpl(HttpPipeline httpPipeline, SerializerAdapter serializerAdapter,
Duration defaultPollInterval, AzureEnvironment environment, String subscriptionId) {
Duration defaultPollInterval, AzureEnvironment environment, String endpoint, String subscriptionId) {
this.httpPipeline = httpPipeline;
this.serializerAdapter = serializerAdapter;
this.defaultPollInterval = defaultPollInterval;
this.endpoint = endpoint;
this.subscriptionId = subscriptionId;
this.apiVersion = "2023-11-01";
this.childResourcesInterfaces = new ChildResourcesInterfacesClientImpl(this);
Expand Down
Loading

0 comments on commit 28d35e5

Please sign in to comment.