|
| 1 | +/* |
| 2 | + * Copyright 2018-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the MIT license. |
| 3 | + */ |
| 4 | + |
| 5 | +package com.microsoft.azure.toolkit.intellij; |
| 6 | + |
| 7 | +import com.azure.core.annotation.*; |
| 8 | +import com.azure.core.http.rest.Response; |
| 9 | +import com.azure.core.http.rest.RestProxy; |
| 10 | +import com.azure.resourcemanager.appservice.models.WebAppBase; |
| 11 | +import com.microsoft.azure.toolkit.lib.appservice.AppServiceAppBase; |
| 12 | +import com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException; |
| 13 | +import reactor.core.publisher.Mono; |
| 14 | + |
| 15 | +import javax.annotation.Nonnull; |
| 16 | +import java.util.Locale; |
| 17 | +import java.util.Objects; |
| 18 | + |
| 19 | +// TODO: Move it to the base plugin (https://github.com/microsoft/azure-maven-plugins/pull/2511) |
| 20 | +public class AppServiceKuduClientExt |
| 21 | +{ |
| 22 | + private final String host; |
| 23 | + private final KuduServiceExt kuduService; |
| 24 | + private final AppServiceAppBase<?, ?, ?> app; |
| 25 | + |
| 26 | + private AppServiceKuduClientExt(String host, KuduServiceExt kuduService, AppServiceAppBase<?, ?, ?> app) { |
| 27 | + this.host = host; |
| 28 | + this.app = app; |
| 29 | + this.kuduService = kuduService; |
| 30 | + } |
| 31 | + |
| 32 | + public static AppServiceKuduClientExt getClient(@Nonnull WebAppBase webAppBase, @Nonnull AppServiceAppBase<?, ?, ?> appService) { |
| 33 | + // refers : https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/resourcemanager/azure-resourcemanager-appservice/src/main/java/ |
| 34 | + // com/azure/resourcemanager/appservice/implementation/KuduClient.java |
| 35 | + if (webAppBase.defaultHostname() == null) { |
| 36 | + throw new AzureToolkitRuntimeException("Cannot initialize kudu client before web app is created"); |
| 37 | + } |
| 38 | + String host = webAppBase.defaultHostname().toLowerCase(Locale.ROOT) |
| 39 | + .replace("http://", "") |
| 40 | + .replace("https://", ""); |
| 41 | + String[] parts = host.split("\\.", 2); |
| 42 | + host = parts[0] + ".scm." + parts[1]; |
| 43 | + host = "https://" + host; |
| 44 | + |
| 45 | + final KuduServiceExt kuduService = RestProxy.create(KuduServiceExt.class, webAppBase.manager().httpPipeline()); |
| 46 | + return new AppServiceKuduClientExt(host, kuduService, appService); |
| 47 | + } |
| 48 | + |
| 49 | + public ExtensionInfo getPackageFromRemoteStore(final @Nonnull String id) { |
| 50 | + var response = Objects.requireNonNull(this.kuduService.getPackageFromRemoteStore(host, id).block()); |
| 51 | + if (response.getStatusCode() == 404) return null; |
| 52 | + |
| 53 | + return response.getValue(); |
| 54 | + } |
| 55 | + |
| 56 | + public ExtensionInfo getInstalledPackage(final @Nonnull String id) { |
| 57 | + var response = Objects.requireNonNull(this.kuduService.getInstalledPackage(host, id).block()); |
| 58 | + if (response.getStatusCode() == 404) return null; |
| 59 | + |
| 60 | + return response.getValue(); |
| 61 | + } |
| 62 | + |
| 63 | + public ExtensionInfo installOrUpdatePackage(final @Nonnull String id) { |
| 64 | + return Objects.requireNonNull(this.kuduService.installOrUpdatePackage(host, id).block()).getValue(); |
| 65 | + } |
| 66 | + |
| 67 | + public void killKuduProcess() { |
| 68 | + killProcess(0); |
| 69 | + } |
| 70 | + |
| 71 | + public void killProcess(final int id) { |
| 72 | + this.kuduService.killProcess(host, id).block(); |
| 73 | + } |
| 74 | + |
| 75 | + @Host("{$host}") |
| 76 | + @ServiceInterface(name = "KuduServiceExt") |
| 77 | + private interface KuduServiceExt { |
| 78 | + @Headers("Content-Type: application/json; charset=utf-8") |
| 79 | + @Get("/api/extensionfeed/{id}") |
| 80 | + @ExpectedResponses({200, 404}) |
| 81 | + Mono<Response<ExtensionInfo>> getPackageFromRemoteStore(@HostParam("$host") String host, @PathParam("id") String id); |
| 82 | + |
| 83 | + @Headers("Content-Type: application/json; charset=utf-8") |
| 84 | + @Get("/api/siteextensions/{id}") |
| 85 | + @ExpectedResponses({200, 404}) |
| 86 | + Mono<Response<ExtensionInfo>> getInstalledPackage(@HostParam("$host") String host, @PathParam("id") String id); |
| 87 | + |
| 88 | + @Headers("Content-Type: application/json; charset=utf-8") |
| 89 | + @Put("/api/siteextensions/{id}") |
| 90 | + Mono<Response<ExtensionInfo>> installOrUpdatePackage(@HostParam("$host") String host, @PathParam("id") String id); |
| 91 | + |
| 92 | + @Delete("/api/processes/{id}") |
| 93 | + @ExpectedResponses({502}) |
| 94 | + Mono<Void> killProcess(@HostParam("$host") String host, @PathParam("id") int id); |
| 95 | + } |
| 96 | +} |
0 commit comments