/*
 * Copyright 2018 Amazon.com, Inc. or its affiliates.
 *
 * 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.
 */

package com.netflix.spinnaker.clouddriver.lambda.deploy.ops;

import com.amazonaws.services.lambda.AWSLambda;
import com.amazonaws.services.lambda.model.DeleteFunctionRequest;
import com.amazonaws.services.lambda.model.DeleteFunctionResult;
import com.netflix.spinnaker.clouddriver.lambda.cache.model.LambdaFunction;
import com.netflix.spinnaker.clouddriver.lambda.deploy.description.DeleteLambdaFunctionDescription;
import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperation;
import java.util.List;

public class DeleteLambdaAtomicOperation
    extends AbstractLambdaAtomicOperation<DeleteLambdaFunctionDescription, DeleteFunctionResult>
    implements AtomicOperation<DeleteFunctionResult> {

  public DeleteLambdaAtomicOperation(DeleteLambdaFunctionDescription description) {
    super(description, "DELETE_LAMBDA_FUNCTION_CODE");
  }

  @Override
  public DeleteFunctionResult operate(List priorOutputs) {
    updateTaskStatus("Initializing deletion of AWS Lambda Function Operation...");
    return deleteFunctionResult();
  }

  private DeleteFunctionResult deleteFunctionResult() {
    String functionName = description.getFunctionName();
    String region = description.getRegion();
    String account = description.getAccount();

    LambdaFunction cache =
        (LambdaFunction) lambdaFunctionProvider.getFunction(account, region, functionName);

    AWSLambda client = getLambdaClient();
    DeleteFunctionRequest request =
        new DeleteFunctionRequest().withFunctionName(cache.getFunctionArn());

    request.withQualifier(description.getQualifier());

    DeleteFunctionResult result = client.deleteFunction(request);
    updateTaskStatus("Finished deletion of AWS Lambda Function  Operation...");

    return result;
  }
}

@@ -2019,24 +2019,70 @@
 daClient();%0A
+%0A    if (cache != null && client != null) %7B%0A  
     DeleteFu
@@ -2113,16 +2113,18 @@
         
+  
 new Dele
@@ -2190,16 +2190,18 @@
 );%0A%0A    
+  
 request.
@@ -2244,24 +2244,26 @@
 er());%0A%0A    
+ 
+ 
 DeleteFuncti
@@ -2312,16 +2312,19 @@
 quest);%0A
+%0A  
     upda
@@ -2396,24 +2396,26 @@
 ...%22);%0A%0A    
+  
 return resul
@@ -2417,14 +2417,38 @@
 result;%0A
+    %7D%0A%0A    return null;%0A
   %7D%0A%7D%0A

Fix NPE when delete operation is called on nonexistent function (#5007)